Last active
May 19, 2022 16:27
-
-
Save MyITGuy/9470051e120ae94e2febfd2f945ccc21 to your computer and use it in GitHub Desktop.
PowerShell: Set-ExitCodeAndExit (ReturnCode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sends exit code to calling shell | |
$host.SetShouldExit([System.Environment]::ExitCode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-ExitCodeAndExit { | |
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="None")] | |
PARAM( | |
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ParameterSetName="p1")] | |
[int] | |
$ExitCode | |
) | |
Write-Verbose "Setting ExitCode to $($ExitCode) and exiting..." | |
# When a PowerShell script is called from a batch file or Command Prompt, this line allows a return code back to the calling shell | |
$host.SetShouldExit($ExitCode) | |
exit $ExitCode | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$host.SetShouldExit()
saved my life on an ancient Win7 PS2.0 machine. A simpleExit 1
would always set the return code ofpowershell.exe -file
to zero ....