I have a XML file look like this:
<Feature>
<B7>A</B7>
<B8>B</B8>
<B9>C</B9>
<ExitCode>
<Found>123</Found>
<NotFound>789</NotFound>
</ExitCode>
</Feature>
I have a PowerShell script look like this:
$regex = [regex]$Pattern
$matching = $regex.Match($FB)
if ($matching.Success) {
while ($matching.Success) {
"Match found: {0}" -f $matching.Value
exit 123 #I want to delete this
$matching = $matching.NextMatch()
}
} else {
"Not Found"
exit 789 #I want to delete this
}
I want to get the exitcode, but I don't want to write exit 123
and exit 780
, I just want to call the exit code from XML file, so everytime I want to change the exitcode number, I just modify from XML file, not from the PowerShell script.
So, I can get the exitcode log if I run the script using batch file, look like this:
set log=C:\Users\Log.txt
set PS=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
set PS=%PS%\powershell.exe -ExecutionPolicy Bypass -File
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File C:\Users\XML.ps1 -i C:\Users\Feature.txt -j C:\Users\XML.xml
echo %ERRORLEVEL% > "%log%"
Something like this; read the XML file, then get the numbers from it by property references:
$xmlData = New-Object -TypeName System.Xml.XmlDocument
$xmlData.Load('c:\test\data.xml')
$regex = [regex]$Pattern
$matching = $regex.Match($FB)
if ($matching.Success) {
while ($matching.Success) {
"Match found: {0}" -f $matching.Value
exit $xmlData.Feature.ExitCode.Found
$matching = $matching.NextMatch()
}
} else {
"Not Found"
exit $xmlData.Feature.ExitCode.NotFound
}
Edit: update for better XML handling, which can handle XML file encodings correctly. Thanks to @tomalak's comments.