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

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

    PowerShell has, at least what I consider, strange behavior when being invoked in this manner. In short, it doesn't treat the command line arguments being passed to powershell.exe as scripts to run. Instead, it treats them as command to run. This is because the default for powershell.exe is -command - see powershell.exe /? for additional info.

    C:\>powershell "'Hello'"
    Hello
    

    What you will need to do, is construct a clever input string to "source" the script you wish to run:

    C:\>powershell ". .\test.ps1"
    Hello, World!
    

    As for the output, once you get the script running correctly, it should just be a matter of capturing STDOUT or whatever ends up fitting with your script.

    Full Example

    test.bat

    @echo off
    powershell.exe ". .\test.ps1"
    

    test.ps1

    "Hello, World!"
    

    Invoke the command:

    test.bat > test.txt
    

    And verify the output was captured:

    C:\>type test.txt
    Hello, World!
    

提交回复
热议问题