sha
sha

Reputation: 75

I need to call .bat file from Powershell script

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

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

I tried as shown below but not working

StartProcess "cmd.exe" "/c D:\EmailConnector\run.bat> $EmailConnector_log"

When I tried as shown below the .bat script ran, but I want to update the log file. Could you help me on this.

StartProcess run.bat -workingdirectory "D:\EmailConnector"

The .bat file consist of jar file for email functionality. But we want to get log in log file.

Upvotes: 0

Views: 17925

Answers (3)

Keith Hill
Keith Hill

Reputation: 201652

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: --%.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

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.

Upvotes: 3

Avshalom
Avshalom

Reputation: 8889

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"

Upvotes: 0

Related Questions