4

I'm investigating a problem on behalf of someone else who is trying to use powershell with one of our products. Our product can execute a command to perform an operation and most folk call an .exe directly which works fine. One user is calling some powershell though, with this method:

powershell -version 2.0 -NonInteractive -ExecutionPolicy ByPass -file ($ScriptsFolder)p4_commit.ps1 {"($Message)" ($Files)}

What happens though is that it never exits, so our software sits and waits forever. The script ends with a simple 'exit 0' and the line immediately before is a debug output which is presented if we cancel the process externally, so it's got to the end of the script OK (and yes, the script runs fine otherwise).

If I remove the exit command and replace it with a powershell session kill:

Stop-Process -Id $PID

Then it does terminate, but of course the exit code is not 0 and we roll back the changes, so it's not a suitable workaround.

Otherwise, back on 'exit 0' I can see that powershell.exe with the parameters sent is still showing up in task manager; so it's not that it's quitting and we're not picking that up, it just doesn't quit.

Alas, i'm not much of a powershell expert- so the question boils down to "What would stop exit exitting?"

James
  • 999
  • 2
  • 11
  • 22
  • Hmm, using Start /wait as described [here][1] seems to help... [1]: http://stackoverflow.com/questions/2041799/powershell-script-gets-stuck-doesnt-exit-when-called-from-batch-file – James Feb 14 '13 at 12:34
  • i guess you never reach the end of the script. By design calling powershell.exe -file ... will automatically close at the end of script execution, unless you explicitly specify -noexit – Loïc MICHEL Feb 14 '13 at 13:14

1 Answers1

2

OK, confirmed, amending the command to call powershell.exe with start /wait fixes it. I have no idea why (it was definitely getting to the end of the script as if I changed the exit to something random i'd get it error out).

Still, problem solved...

James
  • 999
  • 2
  • 11
  • 22
  • You may find Gordon Smith's answer useful: http://stackoverflow.com/questions/2041799/powershell-script-gets-stuck-doesnt-exit-when-called-from-batch-file – John Jul 26 '16 at 23:55