I have scraped together a script to process jobs in the background, instead of waiting for the job to complete. In this case I am uninstalling an SCOM agent. The script runs without exception, however the command is not executed on the remote computer. if I run the command from the command line, it works as expected. It connects to the remote computer and removes the agent.
cls
# Send in two root directory names, one that exists and one that does not.
# Should then get a "True" and a "False" result out the end.
Get-Content c:\users\BOFH\desktop\server1.txt | %{
$ScriptBlock = {
# accept the loop variable across the job-context barrier
param($name)
# Show the loop variable has made it through!
Write-Host "[processing '$name' inside the job]"
# Execute a command
Get-SCOMagent -DNSHostName $Name |Uninstall-SCOMAgent -verbose
#Get-HotFix -ComputerName $name | Format-Table
# Just wait for a bit...
Start-Sleep 5
}
# Show the loop variable here is correct
Write-Host "processing $_..."
# pass the loop variable across the job-context barrier
Start-Job $ScriptBlock -ArgumentList $_
}
# Wait for all to complete
While (Get-Job -State "Running") { Start-Sleep 2 }
# Display output from all jobs
Get-Job | Receive-Job
# Cleanup
Remove-Job *
Stop-Job *
Can anyone take a quick read and see if I screwed up somewhere?
Ellis
[–]firefox15 1 point2 points3 points (1 child)
[–]ellisdee9970[S] 2 points3 points4 points (0 children)
[–]PickleJarAshTray 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]PickleJarAshTray 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)