Calling function in a Parallel foreach loop

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
sekou2331
Posts: 309
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

I know start-sleep. I mean how would I use that in the in the parallel code?

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Just add it to the end of the remote code block.

User avatar
sekou2331
Posts: 309
Joined: Wed Aug 15, 2012 6:07 am

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

Ok thanks. Last question If I run in parallel a trigger to run a schedule task on a remote computer that calls a MSI to install will this have the same issue of dying before completed?

Workflow version
  1. workflow Invoke-Installer
  2.  
  3. {
  4.  
  5.     param (
  6.  
  7.         [string[]]$servers
  8.  
  9.     )
  10.  
  11.     foreach -parallel ($server in $servers)
  12.  
  13.     {
  14.  
  15.         InlineScript
  16.  
  17.         {
  18.  
  19.             $install = {
  20.  
  21.                 Start-ScheduledTask -TaskName "MSI_INSTALLER"
  22.  
  23.             }
  24.  
  25.             Invoke-Command -Scriptblock $install -Computer $using:server
  26.  
  27.         }
  28.  
  29.     }
  30.  
  31. }
  1. | ForEach-Object -Parallel {
  2.  
  3.         Invoke-Command -ComputerName $_  -ScriptBlock {
  4.  
  5.          Start-ScheduledTask -TaskName "MSI_INSTALLER"
  6.  
  7.         }
  8.  
  9.     }

jvierra
Posts: 14676
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: Calling function in a Parallel foreach loop

Post by jvierra »

Yes. The installer woks that way. Without waiting the session will terminate on most MSI installs before the install is complete. You must wait in the session until the install is complete.

You can use ConfigMgr from DSC to ask the remote OS to fully manage and maintain the install.

Locked