I need to call .bat file from Powershell script

后端 未结 3 776
广开言路
广开言路 2020-12-12 07:20

We are migrating perl script to powershell script. In Perl the code is as shown below

$rc=\'D:\\\\EmailConnector\\\\run.bat> $EmailConnector_log;\';


        
相关标签:
3条回答
  • 2020-12-12 07:53

    Use the call operator (&), like this:

    & 'D:\EmailConnector\run.bat' > $EmailConnector_log
    

    The return value of the batch script is automatically put into the variable $LastExitCode.

    0 讨论(0)
  • 2020-12-12 07:53

    Is that what you mean?

    Start-Process "cmd" -ArgumentList '/c','D:\EmailConnector\run.bat' -WorkingDirectory "D:\EmailConnector"
    

    or this one if you need another argument for logfile

    Start-Process "cmd" -ArgumentList '/c','D:\EmailConnector\run.bat','EmailConnector_log' -WorkingDirectory "D:\EmailConnector"
    
    0 讨论(0)
  • 2020-12-12 07:54

    Or, since there are no spaces in the path, you can just execute the batch file directly from PowerShell:

    D:\EmailConnector\run.bat > $EmailConnector_log
    

    This is one of the advantages of PowerShell being both a "shell" and a "scripting language". Execution of batch, cmd, vbs, exe files is straightforward - usually. Parameter passing can be an issue but these days that is easily solved with the stop parsing operator: --%.

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