PowerShell event log xml xpath select not working

蓝咒 提交于 2020-01-13 19:35:09

问题


Would anyone be able to tell my why the selects below don't work? I don't get errors. They just return nothing.

The xml below is the actual event log item converted to xml. I only changed a few values to ensure not private info would be in this post.

$Str represents output from a single event log item. i.e. $event.ToXml().

$str = @"
<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>4624</EventID>
    <Version>0</Version>
    <Level>0</Level>
    <Task>12544</Task>
    <Opcode>0</Opcode>
    <Keywords>0x8020000000000000</Keywords>
    <TimeCreated SystemTime="2014-06-05T12:41:42.490143500Z" />
    <EventRecordID>425650</EventRecordID>
    <Correlation />
    <Execution ProcessID="636" ThreadID="2084" />
    <Channel>Security</Channel>
    <Computer>SERVERHOSTNAME.some.domain.here</Computer>
    <Security />
  </System>
  <EventData>
    <Data Name="SubjectUserSid">S-1-0-0</Data>
    <Data Name="SubjectUserName">-</Data>
    <Data Name="SubjectDomainName">-</Data>
    <Data Name="SubjectLogonId">0x0</Data>
    <Data Name="TargetUserSid">S-1-x-xx</Data>
    <Data Name="TargetUserName">SERVERHOSTNAME$</Data>
    <Data Name="TargetDomainName">TESTDOM</Data>
    <Data Name="TargetLogonId">0x0000000</Data>
    <Data Name="LogonType">3</Data>
    <Data Name="LogonProcessName">Kerberos</Data>
    <Data Name="AuthenticationPackageName">Kerberos</Data>
    <Data Name="WorkstationName"></Data>
    <Data Name="LogonGuid">{00000000-0000-0000-0000-000000000000}</Data>
    <Data Name="TransmittedServices">-</Data>
    <Data Name="LmPackageName">-</Data>
    <Data Name="KeyLength">0</Data>
    <Data Name="ProcessId">0x0</Data>
    <Data Name="ProcessName">-</Data>
    <Data Name="IpAddress">::1</Data>
    <Data Name="IpPort">0</Data>
  </EventData>
</Event>
"@
[xml]$x = $str

#Why isn't this select working?
$x.SelectNodes("/Event/EventData")

#what I am actually trying to accomplish is selecting the value 
#associated to specific attributes, i.e.
$UserSid = $x.SelectSingleNode("/Event/EventData/Data[@name='TargetUserSid']").Value
$UserName = $x.SelectSingleNode("/Event/EventData/Data[@name='TargetUserName']").Value

I am just not enough of an XML guru to understand why these selects are not working. The xml has a namespace (as an attribute in the <event/> tag and is properly formatted.

Any insight into this would be appreciated.


回答1:


This is a classic problem when working with XML; your XML has default namespace (xmlns="...."). The element where the namespace defined and all it's descendants without prefix and without different default namespace declaration considered in the same namespace.

To be able to access elements in a namespace you have to declare a prefix that point to the namespace URI and use that prefix in your XPath query, for example :

.......
$ns = New-Object System.Xml.XmlNamespaceManager($x.NameTable)
$ns.AddNamespace("ns", $x.DocumentElement.NamespaceURI)
$x.SelectNodes("/ns:Event/ns:EventData", $ns)


来源:https://stackoverflow.com/questions/24080627/powershell-event-log-xml-xpath-select-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!