我想解析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