-2

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%"
2

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Don't parse XML by casting the result of Get-Content to [xml]. This is fundamentally wrong. Get-Content has no idea what byte encoding the XML file is in and will happily and silently (!) mangle your data. Always use the XML parser to read the file: $xml = New-Object xml; $xml.Load($path); because encoding detection is a basic feature of XML parsers.
@Tomalak is that any different from any other file reading? Get-Content will do some encoding detection, and only the case where PSv5.1 or below reads a UTF8 file without BOM will you need -Encoding UTF8, and that's the default on PSv6+. That xml reading apparently defaults to UTF8 seems useful to save one parameter on get-content, but not by itself "fundamentally" enough to say "never use the convenient type accelerator"
@Tomalak the default for Get-Content is ASCII on PSv5.1 and below, and UTF8NoBOM on PSv6: learn.microsoft.com/en-us/powershell/module/… ; where did you see that it uses "whatever is default on the machine"? (What even is a "default encoding for a machine"?). If the XML parser can shift encodings mid-stream, that is a good reason to prefer the XML parser, but also a nightmarish horror of XML design.
@tomalak "File encoding detection is a very tricky problem" - it's a stupid problem. "UTF8 has a BOM but it's optional and discouraged" - everything since ~1996 would have been trivial if the BOM was, instead, mandatory. That XML has a place to declare an encoding as all files ought to have, is neat, but it isn't a solved problem. "*I don't trust you to use encodings properly when reading a file .. so I'll solve that by moving the problem, and instead trusting you to have used encodings properly when creating the file". Which, people won't do either. But, eh. What other choice is there.
Please take this discussion to chat. The comments to an answer are hardly the right place for it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.