2013-02-14 122 views
0

任何人都有一個想法如何清除「應用程序和服務日誌」使用Powershell?我可以使用Clear-EventLog輕鬆清除Windows日誌,但我無法清除Windows事件日誌中「應用程序和服務日誌」下的子文件夾。如何使用Powershell清除「應用程序和服務日誌」?

+0

你得到了什麼錯誤? – 2013-02-14 23:35:53

回答

2

這看起來像你需要什麼

http://gallery.technet.microsoft.com/scriptcenter/4502522b-5294-4c31-8c49-0c9e94db8df9

更新 - 這種聯繫有一個登錄。下面是它的腳本 -

Function Global:Clear-Winevent ($Logname) { 
<# 

.SYNOPSIS 
Given a specific Logname from the GET-WINEVENT Commandlet 
it will clear the Contents of that log 

.DESCRIPTION 
Cmdlet used to clear the Windows Event logs from Windows 7 
Windows Vista, Server 2008 and Server 2008 R2 

.EXAMPLE 
CLEAR-WINEVENT -Logname Setup 

.EXAMPLE 
GET-WINEVENT -Listlog * | CLEAR-WINEVENT -Logname $_.Logname 

Clear all Windows Event Logs 

.NOTES 
This is a Cmdlet that is not presently in Powershell 2.0 
although there IS a GET-WINEVENT Command to list the 
Contents of the logs. You can utilize this instead of 
WEVTUTIL.EXE to clear out Logs. Special thanks to Shay Levy 
(@shaylevy on Twitter) for pointing out the needed code 

#> 

[System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("$Logname") 

} 
0

的PowerShell - 性能優化:

版本1:

function Clear-EventLogs-Active 
{ 
    ForEach ($l in (Get-WinEvent).LogName | Sort | Get-Unique) 
    { 
     [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("$l") 
    } 
    Clear-EventLog -LogName "System" 
} 

版本2:

function Clear-EventLogs-All 
{ 
    ForEach ($l in Get-WinEvent -ListLog * -Force) 
    { 
     if ($l.RecordCount -gt 0) 
     { 
      $ln = $l.LogName 
      [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog("$ln") 
     } 
    } 
    Clear-EventLog -LogName "System" 
} 

上514個日誌工作這兩個版本:

  • 版本1(0.3007762秒) - 檢索包含事件

  • 2版(0.7026473秒)僅日誌 - 檢索所有日誌,並且,僅清除有事件的人

相關問題