我尝试分析 Windows 安全日志中的记录,但从某些登录/注销事件中获取特定值时遇到了一些困难。让我们看一个具体的例子 - 这是其中一个日志条目的 XML。
<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-Auditing' Guid='{54849625-5478-4994-a5ba-3e3b0328c30d}'/>
<EventID>4634</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>12545</Task>
<Opcode>0</Opcode>
<Keywords>0x8020000000000000</Keywords>
<TimeCreated SystemTime='2011-08-16T17:15:38.702857400Z'/>
<EventRecordID>107947</EventRecordID>
<Correlation/>
<Execution ProcessID='680' ThreadID='972'/>
<Channel>Security</Channel>
<Computer>SRV1.DOMAIN.LOCAL</Computer><Security/>
</System>
<EventData>
<Data Name='TargetUserSid'>S-1-5-21-963414502-3093649508-813756320-3274</Data>
<Data Name='TargetUserName'>billgates</Data>
<Data Name='TargetDomainName'>MYDOMAIN</Data>
<Data Name='TargetLogonId'>0x1c01acc</Data>
<Data Name='LogonType'>10</Data>
</EventData>
</Event>
例如,我该如何提取0x1c01acc
存储在属性等于的Data
节点中?Name
'TargetLogonId'
答案1
确保您的字符串是有效的 XML(即添加</Event>
到上面发布的内容的末尾,然后将该字符串转换为 XML:
$xml = [xml]$yourStringHere
然后您可以像这样提取 TargetLogonId:
$xml.Event.SelectSingleNode("//*[@Name='TargetLogonId']") | select -ExpandProperty '#text'
感谢 Shay Levy 和这篇文章:http://social.technet.microsoft.com/Forums/en/ITCG/thread/5aa133b0-ea69-4348-9bac-d028ba895024
答案2
如果只提供原始 XML,则可以加载 XML 文档。由于您发布了 XML 片段,因此我假设它是从日志中导出的并且具有<Events>
根标记。棘手的部分是命名空间。
$xml = [xml](get-content "PathToXml.xml")
$xmlns = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable
$xmlns.AddNamespace("el", "http://schemas.microsoft.com/win/2004/08/events/event")
$value = $xml.SelectSingleNode("/Events/el:Event/el:EventData/el:Data[@Name = 'TargetLogonId']/text()", $xmlns).Value
我使用的 XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Events>
<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-Auditing' Guid='{54849625-5478-4994-a5ba-3e3b0328c30d}'/>
<EventID>4634</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>12545</Task>
<Opcode>0</Opcode>
<Keywords>0x8020000000000000</Keywords>
<TimeCreated SystemTime='2011-08-16T17:15:38.702857400Z'/>
<EventRecordID>107947</EventRecordID>
<Correlation/>
<Execution ProcessID='680' ThreadID='972'/>
<Channel>Security</Channel>
<Computer>SRV1.DOMAIN.LOCAL</Computer><Security/>
</System>
<EventData>
<Data Name='TargetUserSid'>S-1-5-21-963414502-3093649508-813756320-3274</Data>
<Data Name='TargetUserName'>billgates</Data>
<Data Name='TargetDomainName'>MYDOMAIN</Data>
<Data Name='TargetLogonId'>0x1c01acc</Data>
<Data Name='LogonType'>10</Data>
</EventData>
</Event>
</Events>
答案3
要获取事件日志条目的 XML:
Get-WInEvent ‹parameters to select the events› | Foreach-Object { $_.ToXml() }
然后使用其他答案中显示的技术来提取特定<Data>
值。