I think that the main issue is the number of databases that you need to backup (both now and in the future), and the impact that they might have on system performance. If you have 50 DB's, you probably don't want to launch them all at the same time.
Try this example. Monitor system performance and vary $RunLimit to find what works best on your system.
Get-Job | Remove-Job -ErrorAction Stop # clear out old job information, stop if there's a problem
$dbs = @('db1','db2','db3','db4','db5','db6','db7','db8','db9','db10','db11','db12','db13','db14','db15') # some bogus names to play with
$RunLimit = 10 # Number of concurrent robocopy's to execute
foreach ($db in $dbs) {
"{0} - Scheduling robocopy for {1}" -f (get-date -Format u), $db
$Atmax = $true # look at how many we have running
while ($Atmax) {
$running = (Get-Job | Where-Object { $_.State -eq 'Running' }) # How many jobs are actively running?
if ($running.Count -le $RunLimit) { # are we at max?
$Atmax = $false # no, it is ok to launch another job
} else {
start-sleep 3 # yes, sleep a bit and look again
}
}
$job = Start-Job -ScriptBlock {
# put real robocopy here
"********************************************************************************************************"
"**** {0}" -f $using:db
"********************************************************************************************************"
Robocopy.exe c:\temp\$($using:db)\ e:\temp\$($using:db)\ *.ini /l /np
}
}
"{0} - All jobs have been launched. " -f (get-date -Format u)
$Atmax = $true # not really max, I just reused the flag to wait for all jobs to complete
while ($Atmax) {
$running = (Get-Job | Where-Object { $_.State -eq 'Running' }) # How many jobs are actively running?
if ($running.Count -eq 0 ) { # are all jobs finished?
$Atmax = $false # yes, we are done
} else {
start-sleep 3 # no, sleep a bit and look again
}
}
"{0} - All jobs have completed. " -f (get-date -Format u)
Get-Job| Receive-Job # retrieve job output. Could be redirected to Out-File