2

I need to call a .bat file from a powershell script synchronously.

I am currently working on a utiity where I need to call a .bat file to generate data in an excel sheet , use powershell script to extract parts of that data and then call .bat file again to processs the extracted data however when the script runs, complete powershell script is executed first and calls to .bat files are made later.

Code snippet:

echo "Hello world"

Start-Process C:/dataloader/bin/process.bat -ArgumentList $tempLibraryPath ExtractTest

Echo "Foobar"

expected output: Hello world call process.bat Foobar

Actual output Hello World Foobar Call process.bat

dev
  • 127
  • 11

1 Answers1

3

You need to tell PowerShell to wait for the process to finish:

Start-Process C:/dataloader/bin/process.bat -ArgumentList $tempLibraryPath ExtractTest -Wait
TobyU
  • 3,718
  • 2
  • 21
  • 32
  • I am still getting the same output. Also of the powershell script: 1. Call process.bat 2. extract details into new excel file from the excel file received from process.bat 3. call process.bat again to work on the details extracted in step 2. whenever I run the [owershell script, I get error on lines responsible for step 2 [file not found] and then code runs process.bat. – dev Sep 05 '18 at 11:19
  • Another solution would be piping the output of the process into Out-Null. Have a look at https://stackoverflow.com/questions/1741490/how-to-tell-powershell-to-wait-for-each-command-to-end-before-starting-the-next – TobyU Sep 05 '18 at 11:23
  • 1
    Worked like a charm. Thanks Toby. – dev Sep 05 '18 at 11:26