I have a situation where a compiled app calls cmd.exe which runs a batch file that calls
powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command “& {.\script.ps1}
script.ps1
has the line $host.setshouldexit($exitcode)
.
But for some reason the powershell.exe process executing the PS1 doesn’t exit. Even if I run
powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command “& {.\script.ps1;Exit $lastexitcode}
it never gets past the PS1 to the Exit command. Adding Exit
to the end of the PS1 doesn't work either. The real annoyance is that all of this works just fine if I manually fire up CMD, open another CMD to replicate the compiled app doing it, and execute the batch file that calls powershell.exe.
cmd.exe /c
orcmd.exe /k
? Also you could just callexit $exitcode
. - Bacon Bits“
>"
- Mattcmd.exe /c
. I could callexit $exitcode
but it isn't always accurate.$host.setshouldexit($exitcode)
gives us consistent results and works everywhere else. It just isn't working when called usingcmd /c
by the app. - arkiados$host.SetShouldExit(1234)
. Watch what happens when I useExit $LASTEXITCODE
. -----cmd /c test.cmd
powershell -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "& {".\Test.ps1";Exit $LASTEXITCODE}"
echo errorlevel: 0
-----cmd /c test.cmd
powershell -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "& {".\Test.ps1"}"
echo errorlevel: 1234
- arkiados