Home > database >  second bat is not executed PowerShell script
second bat is not executed PowerShell script

Time:07-08

the below bat files are being called by PowerShell script, however only bat1.bat is executed the others bat2.bat and last.bat are not being called

#first bat

Start-Process "C:\bat1.bat" -Wait

#run second bat
Start-Process "C:\bat2.bat" -Wait

#run last bat  
cmd.exe /c '\last.bat'

You r support is highly appreciated

CodePudding user response:

Why not try

CALL "C:\bat1.bat"
CALL "C:\bat2.bat"
\last.bat

and avoid Powersmell altogether?

CodePudding user response:

Try using this:

Set-Location C:\temp

Start-Process .\bat1.bat -Wait
Write-Host "bat1.bat DONE"

Start-Process .\bat2.bat -Wait
Write-Host "bat2.bat DONE"

cmd /c .\last.bat
Write-Host "last.bat DONE"

I made the following .bat files:

bat1.bat

@ECHO OFF
ECHO bat1
PAUSE

bat2.bat

@ECHO OFF
ECHO bat2
PAUSE

final.bat

@ECHO OFF
ECHO final

When the PS script was ran, all 3 ran as one would expect.

Putting the Write-Host will show in the console if the previous process finished.

Edit:

I didn't give much explanation before, sorry.

Your issue is that one of the .bat files is not finishing.

The syntax you had should have run bat1.bat and bat2.bat. The "cmd.exe /c "\last.bat" would fail. You need the "c:\last.bat" if you aren't going to set the path to c:. If you did that, you would want ".\last.bat"

To make sure that your script would work, I made 3 basic .bat files whose functions were solely to log a string associated with them. Doing this guaranteed that if anything went wrong, it was with the PS code and not the .bat code.

After verifying your script would run, I made the one I provided. Using Set-location isn't necessary, but I think it makes the code look cleaner. The Write-Host after each process is logging in the PS console that the script is moving on to the next process.

  • Related