how to use powershell delete some specific logs
usually use ' get-eventlog -computername . -list | foreach {$_.log} | foreach {clear-eventlog -comp . -log $_}' to delete logs
but how use powershell delete specific logs such administritive events or setup logs (windows 7)
there's no built-in cmdlet/function that. here's function wrote:
function clear-wineventlog
{
[cmdletbinding(supportsshouldprocess=$true,confirmimpact='high',defaultparametersetname="logname")]
param(
[parameter(
position=0,
mandatory=$true,
parametersetname="logname",
valuefrompipeline=$true,
valuefrompipelinebypropertyname=$true
)]
[string[]]$logname,
[parameter(
position=0,
mandatory=$true,
parametersetname="eventlogconfiguration",
valuefrompipeline=$true
)]
[system.diagnostics.eventing.reader.eventlogconfiguration[]]$eventlog,
[switch]$force
)
process
{
switch($pscmdlet.parametersetname)
{
'logname'
{
write-verbose "parametersetname=logname"
foreach($l in $logname)
{
if($force -or $pscmdlet.shouldprocess($env:computername,"clear event log '$l'"))
{
[system.diagnostics.eventing.reader.eventlogsession]::globalsession.clearlog($l)
}
}
}
'eventlogconfiguration'
{
write-verbose "parametersetname=eventlogconfiguration"
foreach($l in $eventlog)
{
if($force -or $pscmdlet.shouldprocess($env:computername,"clear event log '$($l.logname)'"))
{
[system.diagnostics.eventing.reader.eventlogsession]::globalsession.clearlog($l.logname)
}
}
}
}
}
}
#clear logs (prompts confirmation)
get-winevent -listlog * | clear-wineventlog
# add -force suppress confirmations (or use -whatif:$false)
get-winevent -listlog * | clear-wineventlog -force
shay levy [mvp]
http://blogs.microsoft.co.il/blogs/scriptfanatic
powershell toolbar
Windows Server > Windows PowerShell
Comments
Post a Comment