I have a series of 15 batch files I need to run as part of a powershell script. I currently have a working version of this, but I execute each batch file as below:
Start-Process -FilePath "Script1.bat" -WorkingDirectory "C:\Root" -Wait
Start-Process -FilePath "Script2.bat" -WorkingDirectory "C:\Root" -Wait
Start-Process -FilePath "Script3.bat" -WorkingDirectory "C:\Root" -Wait
and so on.
The batch files are lumping thousands of SQL scripts into one big script, and some of them are much slower than others (the SQL populates 5 different databases and 1 is much larger than the other 4)
I'm trying to improve the speed at which the 15 scripts run, and I thought a good idea would be to run the batch files in parallel so that all of the SQL files are created at the same time, rather than in sequence. To do this I removed the "-Wait"s, and can see all of the command line windows opening simultaneously.
The problem I'm having is that after I have created the SQL files, I'm using Copy-Item to move the scripts to the desired location. Because the Copy-Item commands are no longer waiting for the batch files to execute, it's attempting to copy the SQL files from the folder they are created in before the batch files have finished creating them.
So basically I'm trying to come up with a way to "Wait" for the collection of batch files to run so I can ensure the batch files have finished running before I start copying the files. I've looked online for ages, and have tried using powershell Jobs with the "Wait-Job" command, but the wait only waits until every batch file has been executed, and not until they have been completed. Does anyone have any ideas on how this can be achieved?