需要将XML数据提取到CSV
我需要从事件日志中阅读特定信息。在下面的脚本中,我无法导出XML视图中的某些值包含。在XML视图中,我想要目标用户名:BRSYSAD和MOCKEDUSERNAME:900011-LT
脚本
$filter = "*[System[EventID=4740 and Provider[@Name='Microsoft-Windows-Security-Auditing']]]"
$result = Get-WinEvent -LogName Security -FilterXPath $filter | ForEach-Object {
# convert the event to XML and grab the Event node
$eventXml = ([xml]$_.ToXml()).Event
# output the properties you need
[PSCustomObject]@{
EventID = $eventXml.System.EventID.'#text'
TimeCreated = $eventXml.System.TimeCreated.SystemTime -replace '\.\d+.*$'
Computer = $eventXml.System.Computer
Data = $eventXml.EventData.Data
}
}
# output on screen
$result | Format-Table -AutoSize
# save as CSV file if you like
$result | Export-Csv -Path 'C:\MyProgr_Events_302.csv' -NoTypeInformation
I need to read specific information from event log. in below script I cannot export the some value contains in XML View. In the XML view I Want Target username: BRSYSAD and Subjectusername : 900011-LT
Script
$filter = "*[System[EventID=4740 and Provider[@Name='Microsoft-Windows-Security-Auditing']]]"
$result = Get-WinEvent -LogName Security -FilterXPath $filter | ForEach-Object {
# convert the event to XML and grab the Event node
$eventXml = ([xml]$_.ToXml()).Event
# output the properties you need
[PSCustomObject]@{
EventID = $eventXml.System.EventID.'#text'
TimeCreated = $eventXml.System.TimeCreated.SystemTime -replace '\.\d+.*
Computer = $eventXml.System.Computer
Data = $eventXml.EventData.Data
}
}
# output on screen
$result | Format-Table -AutoSize
# save as CSV file if you like
$result | Export-Csv -Path 'C:\MyProgr_Events_302.csv' -NoTypeInformation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够通过过滤
targetUsername
和offictuSername
属性,通过过滤eventData
的 。示例代码更新(我还删除了
。'#text'
从EventID行中删除了零件,以确保捕获此值),您'd然后最终得到您可用的所有属性,每个结果都包含数据,例如:
![“示例结果数据”]()
Computer = $eventXml.System.Computer TargetUserName = ($eventXml.EventData.Data | Where-Object { $_.Name -eq "TargetUserName"}).'#text' SubjectUserName = ($eventXml.EventData.Data | Where-Object { $_.Name -eq "SubjectUserName"}).'#text' } } # output on screen $result | Format-Table -AutoSize # save as CSV file if you like $result | Export-Csv -Path 'C:\MyProgr_Events_302.csv' -NoTypeInformation如果您愿意,可以使用以下所有属性来删除所有属性:
您'd然后最终得到您可用的所有属性,每个结果都包含数据,例如:
You should be able to get the
TargetUserName
andSubjectUserName
properties by filtering theEventData
for those specifically named attributes.Example code updated (I've also removed the
.'#text'
part from the EventID line to ensure this value is captured)You'd then end up with all the attributes available to you and each result would contain data such as:

Computer = $eventXml.System.Computer TargetUserName = ($eventXml.EventData.Data | Where-Object { $_.Name -eq "TargetUserName"}).'#text' SubjectUserName = ($eventXml.EventData.Data | Where-Object { $_.Name -eq "SubjectUserName"}).'#text' } } # output on screen $result | Format-Table -AutoSize # save as CSV file if you like $result | Export-Csv -Path 'C:\MyProgr_Events_302.csv' -NoTypeInformationIf you prefer, you could pull out all attributes with the following instead:
You'd then end up with all the attributes available to you and each result would contain data such as: