歡迎您光臨本站 註冊首頁

通過PowerShell操作事件日誌

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

通過PowerShell操作事件日誌

管理員能夠獲取信息的主要來源是事件日誌,PowerShell中有專門的Get-EventLog cmdlet處理事件日誌。為了獲取已存在的事件日誌,需要使用-list參數以返回System.Diagnostics.EventLog類型的對象集合。獲取這些對象后即可實現任何與系統日誌相關聯的操作,如下所示:

從下例的輸出能夠看到當前系統中存在的日誌條數:
PS C:\PowerShell\AppendixB> get-eventlog -list
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
512 7 OverwriteOlder 486 Application
512 7 OverwriteOlder 0 Internet Explorer
512 7 OverwriteOlder 1 Security
512 7 OverwriteOlder 2,166 System
15,360 0 OverwriteAsNeeded 2,148 Windows PowerShell
一、獲取特定的事件日誌

首先獲取關於PowerShell系統日誌的日誌對象:
PS C:\PowerShell\AppendixB> $log = get-eventlog -list |
>> ? { $_.logdisplayname -like "*Pow*" }
>>
接下來檢查獲取的日誌是否正常:
PS C:\PowerShell\AppendixB> $log.LogDisplayName
Windows PowerShell
隨後查看最近發生的5條系統日誌:
PS C:\PowerShell\AppendixB> get-eventlog $log.LogDisplayName -newest 5
Index Time EntryType Source InstanceID Message
----- ---- --------- ------ ---------- -------
2148 九月 20 10:06 Information PowerShell 400 Engine state is changed fro...
2147 九月 20 10:06 Information PowerShell 600 Provider "Certificate" is S...
2146 九月 20 10:06 Information PowerShell 600 Provider "Variable" is Star...
2145 九月 20 10:06 Information PowerShell 600 Provider "Registry" is Star...
2144 九月 20 10:06 Information PowerShell 600 Provider "Function" is Star...
查看系統日誌最大的容量:
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes
15360
從中能夠看到是15 MB,然後加倍系統允許的最大日誌大小:
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes *= 2
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes
30720
二、將事件日誌作為實時對象

EventLog對象的主要特點是其實時性,即一旦獲取這個對象,則可不斷地檢查它,以查看是否發生了新的事件。例如,可以查看保存在$log變數中的PowerShell日誌:
PS C:\PowerShell\AppendixB> $log
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
30,720 0 OverwriteAsNeeded 2,148 Windows PowerShell
能夠看到當前的日誌條數是2 148條。下面增加啟動多個PowerShell實例增加多條日誌,這裡向PowerShell實例傳遞了exit命令,每個新實例在啟動之後立即退出:
PS C:\PowerShell\AppendixB> powershell exit
PS C:\PowerShell\AppendixB> powershell exit
PS C:\PowerShell\AppendixB> powershell exit
下面再次查看$log的屬性:
PS C:\PowerShell\AppendixB> $log
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
30,720 0 OverwriteAsNeeded 2,187 Windows PowerShell
可以看到日誌中已經添加了多條新紀錄。接下來清理已經添加的日誌,執行此操作通過單獨啟動PowerShell實例清除現有PowerShell的日誌,命令如下:
PS C:\PowerShell\AppendixB> powershell {
>> (get-eventlog -list |
>> ?{$_.LogDisplayName -like "*Pow*"}).Clear()
>> }
>>
其中的命令傳遞腳本塊給一個新的PowerShell進程,這個腳本塊獲取PowerShell EventLog對象並調用Clear()方法清除已有的日誌,在子進程結束之後查看當前的日誌:
PS C:\PowerShell\AppendixB> $log
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
30,720 0 OverwriteAsNeeded 1 Windows PowerShell
可以看到PowerShell的日誌已經被清空。
三、保存事件日誌

可以通過PowerShell的Export-Clixml cmdlet保存事件日誌以便於後期處理,導出日誌的命令如下所示:
PS C:\PowerShell\AppendixB> $log.Entries | Export-Clixml c:\log.xml
這裡通過命令將數據再次讀出日誌:
PS C:\PowerShell\AppendixB> $date = Import-Clixml C:\log.xml
為了對比從實時對象讀取的日誌,以及從外部讀入的日誌的不同,下面輸出實施日誌的信息:
PS C:\PowerShell\AppendixB> $log.Entries |
>> ft -auto Index,Time,EventID,Message
>>
Index Time EventID Message
----- ---- ------- -------
1 403 Engine state is changed from Available to StoppeD- ...
2 600 Provider "WSMan" is StarteD- ...
3 600 Provider "Alias" is StarteD- ...
4 600 Provider "Environment" is StarteD- ...
從外部再次讀入的數據記錄如下:
PS C:\> $data |
>> ft -auto Index,Time,EventID,Message
>>
Index Time EventID Message
----- ---- ------- -------
1 403 Engine state is changed from Available to StoppeD- ...
2 600 Provider "WSMan" is StarteD- ...
3 600 Provider "Alias" is StarteD- ...
4 600 Provider "Environment" is StarteD- ...
兩次輸出的內容或多或少相同。當然讀入的數據與實時對象有所不同,它不再是實時對象。沒有任何方法,對其屬性的修改也不會反作用於系統。
Get-MachinesMissingHotfix.ps1腳本的代碼如下所示:
get-process | foreach {$processes = @{}} {
$processes[$_.processname] = $_}
get-service |
where {$_.Status -match "running" –and
$_.ServiceType -eq "Win32OwnProcess" } |
foreach {
new-object psobject |
add-member -pass NoteProperty Name $_.Name |
add-member -pass NoteProperty PID $processes[$_.Name].Id |
add-member -pass NoteProperty WS $processes[$_.Name].WS |
add-member -pass NoteProperty Description $_.DisplayName |
add-member -pass NoteProperty FileName `
$processes[$_.Name].MainModule.FileName
} |
export-csv -notype ./service_datA.csv

[火星人 ] 通過PowerShell操作事件日誌已經有1751次圍觀

http://coctec.com/docs/service/show-post-2568.html