我想解析XML文件在关键字定义的目录中。 如果找到关键字,则应先替换它以避免进一步匹配,然后将xml的内容作为事件的消息部分发送。Powershell XML解析和写入事件查看器
主要问题在于最后一个代码块,它将逐行解析.xml而不是块。
#### THIS CODE GETS ALL THE FILES THAT CONTAINS THE "Major" Pattern
$Path = "C:\test\"
$SearchFor = "Major"
$TestFor = "parsedxml"
$Parse = "ParsedXML"
$PathArray = @()
$FolderFile = "C:\test\*.xml"
$found = @()
# This code snippet gets all the files in $Path that end in ".xml".
Get-ChildItem $Path -Filter "*.xml" | Select-String -Pattern "Major"
ForEach-Object {
If (Get-Content $FolderFile | Select-String -Pattern
"Major","Parsedxml")
{
}
}
#### THIS WORKS TO CHANGE THE KEYWORD IN THE FILE ###
Get-ChildItem C:\test\*.xml -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace "Major", "Parsed"}) | Set-Content $_
}
### THIS WORKS (KINDA) TO PARSE THE FILE INTO AN EVENTLOG ###
### BUT IT PARSES THE .XML LINE BY LINE FOR SOME REASON ####
Get-ChildItem C:\test\*.xml | ForEach {
(Get-Content $_)| ForEach { Write-EventLog –LogName Application –Source “Verint Alert” –EntryType Information –EventID 1 -Message ("Triggered Alarm" + ($_))
}
}
但我似乎无法使代码执行以下操作: 读取该文件,如果它包含“主要”分析整个.XML作为“写入事件日志-Message”,一旦被解析,变化关键字Major到Parsed这个词。通过文件$的每一行
(Get-Content $_)| ForEach { ... }
将循环_:
顺便说一句,我很确定你的“主要”模式是在一个特定的XML标签中,你应该能够使用XML格式和XPath工作而不是原始的gc和选择字符串 –
还不熟悉XPath ..这是我被扔在我身上,我不得不尝试快速皮卡.. – Delaney