I have a command that I want to run against multiple SQL servers. To make the script run quicker I'd like to use background jobs. When I run the command outside of a job it completes and gives me the output I expect. When I run the command inside of a job it gets stuck in a "running" state.
This command runs fine:
$Serverlist = 'server1','server2'
foreach ($Server in $ServerList) {
$con = "server=$Server;database=master;Integrated Security=sspi"
$cmd = "EXEC master.dbo.xp_msver"
$da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
$dt = new-object System.Data.DataTable
$da.fill($dt)
$dt
}
However, this command creates the background jobs but they never complete
$Serverlist = 'server1','server2'
foreach ($Server in $ServerList) {
Start-Job -ArgumentList $Server -ScriptBlock {
param ($Server)
$con = "server=$Server;database=master;Integrated Security=sspi"
$cmd = "EXEC master.dbo.xp_msver"
$da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
$dt = new-object System.Data.DataTable
$da.fill($dt)
$dt
}
}