Powershell script gets stuck, doesn't exit when called from batch file

前端 未结 10 575
不知归路
不知归路 2020-12-24 11:08

I have a PowerShell script that connects to a web site, and parses its returned data (It\'s about importing a previously uploaded SQL file into the web site\'s data base). T

相关标签:
10条回答
  • 2020-12-24 11:37

    IF that is exactly what's in your file it's because you have mismatched quotes. Powershell is waiting for the last quote.

    0 讨论(0)
  • 2020-12-24 11:37

    The problem I bet is that the Powershell process continutes to run after executing the script. This means it has not exited and therefore there is no exit code. I'm having a similar problem when trying to run a Powershell script from C# that does stuff when finished, except it never finishes...

    Hacky, but the only solution I've found is to put Stop-Process -processname powershell at the end of the .ps1 script. This kills off the PowerShell process (and all PowerShell windows you have open unfortunately) but seems to work. Might work for your script as well.

    0 讨论(0)
  • 2020-12-24 11:41

    you can simply add an 'exit' command to the end of the .ps1 script (or any variant of process termination that you wish). Powershell will continue to run at the end of the script as it has not been told to terminate (when run in PS or ISE it will auto-terminate at the end of the script but not when run through the DOS shell).

    0 讨论(0)
  • 2020-12-24 11:43

    I had the exact issue, we were trying to integrate power shell scripts into another system and it kept giving a timeout error. Probably because of the issue mentioned by Gordon Smith, his solution might work. But to me I prefer to have complete control of my script from within the script itself and not depend on the way in which it is called.

    $pid is built in variable with the PID. The below will end the current powershell process and leave the others intact. This way anyone can call your script as normal and it will work as expected.

    Stop-Process $pid
    
    0 讨论(0)
  • 2020-12-24 11:47

    From my experience, PowerShell.exe can easily run scripts from within a batch file or shell script in a predictable way using the -File switch. One does not need to use the Start command.

    The important thing to do is to append

    < nul
    

    to the command line from within a batch file. My research has shown that PowerShell runs the commands in the script indicated through the -File switch and then waits for additional PowerShell commands from the standard input (my brief experimentation with the -Command switch demonstrated similar behavior). By redirecting the standard input to nul, once PowerShell finishes executing the script and "reads end-of-file" from the standard input, PowerShell exits.

    When invoking PowerShell from Cygwin, use

    < /dev/null
    

    For example, I've run PowerShell scripts from Cygwin using shell variables, like this:

    PowerShell.exe -ExecutionPolicy RemoteSigned -File $_powershellscriptpath $_firstscriptparameter < /dev/null
    

    Please post a comment if your experience varied from mine. - Gordon

    0 讨论(0)
  • 2020-12-24 11:47

    If you want to capture the output of the powershell.exe commands then you can also use the /B parameter to force the process to run in the same command shell.

    We've just seen a very odd instance of this problem. A batch file containing the call powershell.exe -command ... ran fine locally but stalled as described above when the batch file was run in the context of an msdeploy -postSync command. After experimenting with process.Kill() to force PowerShell to quit, we lit on the use of START /WAIT /B PowerShell.exe ..

    No idea why this should work, but it does ...

    0 讨论(0)
提交回复
热议问题