I want to run the script from command line, and get the error level. I use this batch script.
set log=C:\Users\PowerShell\XML\Log.txt
set PS=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
set PS=%PS%\powershell.exe 
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" C:\Users\XML.ps1 -i C:\Users\A2.txt -j C:\Users\Fb.xml
   
echo %ERRORLEVEL% > "%log%"
I tried this script to get the custom errorlevel, If it is found, it will return 123, otherwise 456. But It always gives me 1.
$regex = [regex]$Pattern
$matching = $regex.Match($FBString)
if ($matching.Success) {
    while ($matching.Success) {
        "Match found: {0}" -f $matching.Value
        Exit 123
        $matching = $matching.NextMatch()
    }
}
else {
    "Not Found"
    Exit 456
}
