Foreach -parallel

Welcome Forums General PowerShell Q&A Foreach -parallel

Viewing 3 reply threads
  • Author
    Posts
    • #220686
      Participant
      Topics: 1
      Replies: 1
      Points: -5
      Rank: Member

      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

      }

      • This topic was modified 1 month ago by Fentrez. Reason: adding better topic title
      • This topic was modified 1 month ago by Fentrez.
    • #221517
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      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.

    • #221529
      Participant
      Topics: 1
      Replies: 1
      Points: -5
      Rank: Member

      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.

      • This reply was modified 1 month ago by Fentrez.
    • #221541
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      I apologize I missed that.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.