在开始本文之前,首先需要了解日志的tracing level,windows的tracing level分为0-4五个级别,分别代表的内容为:
0 debug
1 info
2 warning
3 error
4 fatal
1 info
2 warning
3 error
4 fatal
在PS 2.0中,处理日志的cmdlet包括Get-Event、Get-EventLog、Get-WinEvent三个,以下以Get-WinEvent为例进行演示,其他两个语法大致相同,请自行查看。
1.获取system日志中tracing level为fatal的所有日志发生的机器名
Get-WinEvent -LogName system | where{$_.level -eq 4}| Select-Object MachineName -Unique
2.获取powershell日志的数量,分类别查看日志数量
$events = get-winevent -logname "Windows PowerShell"
$events.count
$events | group-object id -noelement | sort-object count -desc
$events | group-object leveldisplayname -noelement
$events.count
$events | group-object id -noelement | sort-object count -desc
$events | group-object leveldisplayname -noelement
3.查看指定位置的日志文件中指定id的事件日志
get-winevent -path "c:\tracing\tracelog.etl", "c:\Logs\Windows PowerShell.evtx" -oldest | where {$_.id -eq "103"}
4.获取powershell日志从昨天以来的事件
$yesterday = (get-date) - (new-timespan -day 1)
get-winevent -logname "Windows PowerShell" | where {$_.timecreated -ge $yesterday}
get-winevent -logname "Windows PowerShell" | where {$_.timecreated -ge $yesterday}
5.获取powershell日志从昨天以来、tracing level为error 的事件
$yesterday = (get-date) - (new-timespan -day 1)
get-winevent -FilterHashTable @{LogName='Windows PowerShell'; Level=3; StartTime=$yesterday}
get-winevent -FilterHashTable @{LogName='Windows PowerShell'; Level=3; StartTime=$yesterday}
转载于:https://blog.51cto.com/shawnshi/336320