2

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
}

4 Answers 4

2

Change the Exit to Return . Exit will exit your script. Return will return values that you wish for.

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

5 Comments

It works when I run it from powershell. But I want to get the errorlevel value by using batch file
0 indicates that script ran successfully and 1 indicates that there is an error. If you want to capture the same in batch then you have redirect them to stdout
( exit <n> ) set the system %errorlevel% environment variable... while ( return <n> ) will try to pass the object <n> to the calling code... or by default write it to out-default... so is you want to use batch files or other executables to call your powershell code and interact with the result in the OS standard way... you should use exit <n> ;-)
Unfortunately, I have to disagree, as your answer is not true (at least not any longer). Using exit \b 123 is best practice for exit-codes since windows 2000/xp (pretty much at the button of this link).
The question is about the process exit code set by powershell.exe (which cmd.exe reflects in %ERRORLEVEL%), which you can only set with exit. By contrast, passing an argument to return outputs the latter as data (to stdout, from an external caller's perspective).
2

You can write the logic in your script
so any exit point will set the %errorlevel% system variable...

and create your "error return logic"...

something lihe this:

if ( $LASTEXITCODE -eq 0 ) { exit 0  }
if ( $LASTEXITCODE -eq 1 ) { exit 1  }
if ( $x -gt 1 ) { exit 2  }
if ( $x -lt 1 ) { exit 3  }
...

and then you can inspect from a batch file for %errorlevel% == 0, 1, 2, ...

Comments

1

Run the script with powershell -file. The default is powershell -command. Once that script is over, with -command the session is still going.

2 Comments

Thank you very much! This saved my day ... and should be the accepted answer (no rewriting of scripts, the desired exit code directly put into %errorlevel%).
Good point re -File vs. -Command, but what do you mean by "the session is still going"?
1

The problem is in the batch file. It should be like this :

set log=C:\Users\PowerShell\XML\Log.txt    

# Note the use of -File
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File C:\Users\XML.ps1 -i C:\Users\A2.txt -j C:\Users\Fb.xml

echo %ERRORLEVEL% > "%log%"

For the first PowerShell script is correct already.

1 Comment

Indeed, but to spell it out: To make the PowerShell CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7) report the exit code of a script file (*.ps1) as the CLI's process exit code, use the -File parameter (implied with pwsh). If you use -Command (implied with powershell.exe), the exit code gets mapped onto 0 or 1, based on the abstract success status of the script (of the last command executed). For a comprehensive overview of PowerShell's exit-code handling, see this answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.