Foreach -parallel

Hi,

 

I have a script I put together to update multiple servers at the same time, however some get stuck on the " Invoke-Command -Session $session {Wait-Process powershell}" bit and to find out which server is holding up the batch we have to login to each server and see if a powershell.exe is running then terminate it if its failed, I tried to edit it by putting another write-output after, however of course it is running -parallel so it waits for powershell.exe process to self terminate on all servers.

 

Anyone know a better way of handling this, would be great if it just ran the all at same time, end to end, and not wait for any in the batch, but again at a bit of a loss on that one.

 

I’ve removed alot of the script as not relevant

workflow update-servers
{

Param
(
$updateset

)

foreach -parallel -throttlelimit 10 ($vm in $vms)
{

sequence
{
if(Test-Connection $vm.DNSHostname -Count 1 -Quiet)
{
Write-Output "output"
Write-Output "output" | Out-File $log -Append
if(Test-WSMan $vm.DNSHostname -ErrorAction SilentlyContinue)
{
write-output "output"
Write-Output "output" | Out-File $log -Append
inlinescript {
$session = New-PSSession -ComputerName $using:vm.dnshostname
Invoke-Command -Session $session {Stop-Process -Name powershell -Force -ErrorAction SilentlyContinue}
}
InlineScript {
Import-Module PSWindowsUpdate
Invoke-WUInstall -ComputerName $using:vm.DNSHostname -Script {ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll -IgnoreReboot | Out-File C:\temp\Update.log} -Confirm:$false

$session = New-PSSession -ComputerName $using:vm.dnshostname
Invoke-Command -Session $session {Wait-Process powershell}
Write-Output "output"
Write-Output "output" | Out-File $log -Append
}
Checkpoint-Workflow

}

You could just verify wsman is responding prior to trying to execute against that server.

if($(
test-wsman -ComputerName "dontexist"
)){
"run code"
}
else{
"dont run code"
}

It will output error text if it fails, but since it’s in a workflow I’m assuming that won’t matter. If you need to suppress it you could do something like this.

if($(
try{
test-wsman -ComputerName "dontexist" -ErrorAction Stop
}
catch{}
)){
"run code"
}
else{
"dont run code"
}

I hope this helps.

Hello,

 

I do verify this if(Test-WSMan $vm.DNSHostname -ErrorAction SilentlyContinue)

 

There is a else command further down to state remote powershell failed.

 

The problem is Invoke-Command -Session $session {Wait-Process powershell}

Sometimes the powershell process will sit there and fail for one reason or another.

I could however put a start-sleep in there to give the powershell time to execute, check if the output log C:\temp\Update.log exists and has content, if it doesnt then report it to stop the batch being held up. I could also get the last line of the log to update the process until it reads ready to reboot in the log. But that may be another release of this script.

I apologize I missed that.