Skip to content

Instantly share code, notes, and snippets.

@davops
Created June 28, 2016 19:50
Show Gist options
  • Save davops/95461ad6503b977b282739ab224767f5 to your computer and use it in GitHub Desktop.
Save davops/95461ad6503b977b282739ab224767f5 to your computer and use it in GitHub Desktop.
Using PowerShell, start background jobs on multiple servers at the same time and wait until they finish.
#Purpose: Start background jobs on multiple servers at the same time and wait until they finish
$servers = "server1","server2","server3"
$buildNumber = "1.2"
$servers | %{
$script =
{
param($server)
Invoke-Command -ComputerName $server -ScriptBlock {param($buildNumber) New-Item c:\Deployment\$buildNumber -type directory -force}
Invoke-Command -ComputerName $server -ScriptBlock {param($server) Start-Process -Wait -Filepath "C:\Deployment\Deployment_Script.exe" -RedirectStandardOutput "c:\deployment\output_$server.txt"} -Args $server
}
Start-Job $script -ArgumentList $_ #refers to the $server variable
}
While(Get-Job -State "Running"){Start-Sleep 30}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment