管理員能夠獲取信息的主要來(lái)源是事件日志,PowerShell中有專門(mén)的Get-EventLog cmdlet處理事件日志。為了獲取已存在的事件日志,需要使用-list參數(shù)以返回System.Diagnostics.EventLog類型的對(duì)象集合。獲取這些對(duì)象后即可實(shí)現(xiàn)任何與系統(tǒng)日志相關(guān)聯(lián)的操作,如下所示:
從下例的輸出能夠看到當(dāng)前系統(tǒng)中存在的日志條數(shù):
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
一、獲取特定的事件日志
首先獲取關(guān)于PowerShell系統(tǒng)日志的日志對(duì)象:
PS C:\PowerShell\AppendixB> $log = get-eventlog -list |
>> ? { $_.logdisplayname -like "*Pow*" }
>>
接下來(lái)檢查獲取的日志是否正常:
PS C:\PowerShell\AppendixB> $log.LogDisplayName
Windows PowerShell
隨后查看最近發(fā)生的5條系統(tǒng)日志:
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...
查看系統(tǒng)日志最大的容量:
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes
15360
從中能夠看到是15 MB,然后加倍系統(tǒng)允許的最大日志大小:
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes *= 2
PS C:\PowerShell\AppendixB> $log.MaximumKilobytes
30720
二、將事件日志作為實(shí)時(shí)對(duì)象
EventLog對(duì)象的主要特點(diǎn)是其實(shí)時(shí)性,即一旦獲取這個(gè)對(duì)象,則可不斷地檢查它,以查看是否發(fā)生了新的事件。例如,可以查看保存在$log變量中的PowerShell日志:
PS C:\PowerShell\AppendixB> $log
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
30,720 0 OverwriteAsNeeded 2,148 Windows PowerShell
能夠看到當(dāng)前的日志條數(shù)是2 148條。下面增加啟動(dòng)多個(gè)PowerShell實(shí)例增加多條日志,這里向PowerShell實(shí)例傳遞了exit命令,每個(gè)新實(shí)例在啟動(dòng)之后立即退出:
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
可以看到日志中已經(jīng)添加了多條新紀(jì)錄。接下來(lái)清理已經(jīng)添加的日志,執(zhí)行此操作通過(guò)單獨(dú)啟動(dòng)PowerShell實(shí)例清除現(xiàn)有PowerShell的日志,命令如下:
PS C:\PowerShell\AppendixB> powershell {
>> (get-eventlog -list |
>> ?{$_.LogDisplayName -like "*Pow*"}).Clear()
>> }
>>
其中的命令傳遞腳本塊給一個(gè)新的PowerShell進(jìn)程,這個(gè)腳本塊獲取PowerShell EventLog對(duì)象并調(diào)用Clear()方法清除已有的日志,在子進(jìn)程結(jié)束之后查看當(dāng)前的日志:
PS C:\PowerShell\AppendixB> $log
Max(K) Retain OverflowAction Entries Name
------ ------ -------------- ------- ----
30,720 0 OverwriteAsNeeded 1 Windows PowerShell
可以看到PowerShell的日志已經(jīng)被清空。
三、保存事件日志
可以通過(guò)PowerShell的Export-Clixml cmdlet保存事件日志以便于后期處理,導(dǎo)出日志的命令如下所示:
PS C:\PowerShell\AppendixB> $log.Entries | Export-Clixml c:\log.xml
這里通過(guò)命令將數(shù)據(jù)再次讀出日志:
PS C:\PowerShell\AppendixB> $date = Import-Clixml C:\log.xml
為了對(duì)比從實(shí)時(shí)對(duì)象讀取的日志,以及從外部讀入的日志的不同,下面輸出實(shí)施日志的信息:
PS C:\PowerShell\AppendixB> $log.Entries[0..3] |
>> 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- ...
從外部再次讀入的數(shù)據(jù)記錄如下:
PS C:\> $data[0..3] |
>> 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- ...
兩次輸出的內(nèi)容或多或少相同。當(dāng)然讀入的數(shù)據(jù)與實(shí)時(shí)對(duì)象有所不同,它不再是實(shí)時(shí)對(duì)象。沒(méi)有任何方法,對(duì)其屬性的修改也不會(huì)反作用于系統(tǒng)。
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
作者: 付海軍
出處:http://fuhj02.cnblogs.com
版權(quán):本文版權(quán)歸作者和博客園共有
轉(zhuǎn)載:歡迎轉(zhuǎn)載,為了保存作者的創(chuàng)作熱情,請(qǐng)按要求【轉(zhuǎn)載】,謝謝