Saturday, 18 April 2015

Powershell: XML Parsing and writing to the event viewer



I am trying to parse an XML files from a directory where a keyword is defined, if the keyword is found true, take the contents of the xml and post it as the -Message portion of a Write Event and change that keyword so it isn't found the second time the script is run.





#### 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" + ($_))
}
}



But I cannot seem to make the code do the following: Read the file, if it contains "Major" Parse the whole .xml as a "Write EventLog -Message" and once it is parsed, change the keyword Major to the word Parsed.


No comments:

Post a Comment