Powershell 查询 Windows 日志的方法

553 查看

论坛里面有人询问如何使用powershell脚本查询文件修改的审计日志,豆子服务器没开这个功能,不过尝试写了个类似的脚本可以查询日志,并输出对应的xml内容。

基本方法是get-winevent, 可以指定对应的eventid,获取列表。如果想获取这个事件具体的内容,需要根据不同事件的xml内容进行变化。

比如

$Events = Get-WinEvent -ComputerName syddc01 -FilterHashtable @{Logname='Security';Id=4771} -MaxEvents 1 
$eventXML = [xml]$Event.ToXml() 
$eventxml.event.event.data 

wKioL1X4-cOxNGptAADLqd6u8fc788.jpg

根据这个思路,我如果想获取最新的20个4771的事件日志,并输出结果

$Events = Get-WinEvent -ComputerName syddc01 -FilterHashtable @{Logname='Security';Id=4771} -MaxEvents 20     
       
# Parse out the event message data       
ForEach ($Event in $Events) {       
  # Convert the event to XML       
  $eventXML = [xml]$Event.ToXml()       
  # Iterate through each one of the XML message properties       
  For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) { 
    
       
    # Append these as object properties       
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $eventXML.Event.EventData.Data[$i].name -Value $eventXML.Event.EventData.Data[$i].'#text'      
  }       
}       
  
   
$events | select Message, TargetUserName, ipaddress,timecreated | Out-GridView 

wKioL1X4-iPDwCAuAANhZPcU2WY929.jpg

有的时候,事件的数目很多,我希望对这个时间进行一个限制。千万别用 where-object 的方式来过滤,不然等到地老天荒也未必出结果。
我们需要通过哈希表来过滤

$endtime=get-date
$starttime=$endtime.addminutes(-1) 
$eventcritea = @{logname='security';id=4740;starttime=$starttime;endtime=$endtime}

另外一种常见的方式是通过xmlfilter来过滤日志

首先,我们可以通过event viewer来自定义一个xpath

wKioL1X4-uqzi5EaAADOjL7kW3U689.jpg

wKiom1X4-UWAiui2AAGGHaznvrM148.jpg

wKioL1X4-3yxulR8AAEV0mqB1HM575.jpg

因为是不同的事件,他的eventdata结果是不一样的,因此我做了些变动。

[xml]$xmlFilter = @" 
<QueryList> 
 <Query Id="0" Path="Application"> 
  <Select Path="Application">*[System[(EventID=1002) and TimeCreated[timediff(@SystemTime) <= 604800000]]]</Select> 
 </Query> 
</QueryList> 
“@ 
#Get-WinEvent -ComputerName $DC.DC -LogName Security -FilterXPath "*[System[(EventID=529 or EventID=644 or EventID=675 or EventID=676 or EventID=681 or EventID=4625) and TimeCreated[timediff(@SystemTime) <= 86400000]]]" #-MaxEvents 50 
$Events = Get-WinEvent -ComputerName syddc01 -FilterXML $xmlFilter 
ForEach ($Event in $Events) {       
  # Convert the event to XML       
  $eventXML = [xml]$Event.ToXml()       
  # Iterate through each one of the XML message properties       
  For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) { 
    
       
    # Append these as object properties       
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name "App" -Value $eventXML.Event.EventData.Data[5]      
  }       
}   
$Events | select Message, App, providerName, timecreated | Out-GridView 

结果如下

wKiom1X4-LfAh_1ZAAIHTGF2q2I120.jpg

最后再给一个例子,我希望获取lockout用户的信息以及他们是在哪里被锁住的,这个日志我们查看4771或者4740。4771的日志过多,查询太慢,所以这里我已4740为例。

eventcritea = @{logname='security';id=4740} 
$Events =get-winevent -ComputerName (Get-ADDomain).pdcemulator -FilterHashtable $eventcritea 
#$Events = Get-WinEvent -ComputerName syddc01 -Filterxml $xmlfilter     
       
# Parse out the event message data       
ForEach ($Event in $Events) {   
    
  # Convert the event to XML       
  $eventXML = [xml]$Event.ToXml()   
      
  # Iterate through each one of the XML message properties       
  For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) { 
    
       
    # Append these as object properties       
    Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name $eventXML.Event.EventData.Data[$i].name -Value $eventXML.Event.EventData.Data[$i].'#text'      
  }       
}       
  
   
$events | select TargetUserName,timecreated, targetdomainname | Out-GridView -Title LockOutStatus 
break; 
Search-ADAccount -LockedOut | ForEach-Object {Unlock-ADAccount -Identity $_.distinguishedname }

wKioL1YCTLTT2ydOAAKYKU8Qewo401.jpg

本文出自 “麻婆豆腐” 博客