0

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.

1
  • I wrote an answer, however, your question is not clear enough, the log file should be the input for the run.bat? or it just need to run from the d:\emailconnector folder? Commented Jul 5, 2015 at 8:20

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

& 'D:\EmailConnector-Disc Optimus\run.bat' >$emailconnecter_log i tried like this and got an error message as run.bat : log4j:ERROR setFile(null,true) call failed. At D:\EmailConnector-Disc Optimus\Output\Test_Eamil.ps1:108 char:3 + & <<<< 'D:\EmailConnector-Disc Optimus\run.bat' >$emailconnecter_log + CategoryInfo : NotSpecified: (log4j:ERROR set...e) call failed.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
That error comes from the code in your batch file and is unrelated to this question. Please post a new question with the content of the batch file and the error message.
0

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"

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.