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

前端 未结 10 645
不知归路
不知归路 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: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

提交回复
热议问题