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 »

So I was trying the below. I get no error but it seems not to work.
  1. 'server1', 'server2' | ForEach-Object -Parallel {
  2.  
  3.     $Software = 'SoftwareName.msi'
  4.  
  5.     Invoke-Command -ComputerName $_ -ScriptBlock {
  6.  
  7.         $softwareFormat = "$using:Software"
  8.  
  9.         $arguments = '/qn /log D:\theLog.log PRODUCT_INSTALLDIR="D:\Program Files\"'
  10.  
  11.         Start-Process -FilePath $softwareFormat -ArgumentList $arguments -PassThru
  12.  
  13.     }
  14.  
  15. }

jvierra
Posts: 14672
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 »

Syntax errors. You need to learn to not use quotes because th3ey look good.

Remoting an installer will not work because the session is being closed before the install is complete.

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 where am I using quotes wrong and the install takes some time. How can I run the installer in parallel without it dying before completed?

jvierra
Posts: 14672
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 »

That depends on the installer. When the session ends the install will be cancelled. You will need to contact the vendor to find out how to wait on the installer before ending the session. Some installers can be encouraged to wait until the install has completed or failed. Others will require that you monitor the install log to detect completion.

It is never necessary to quote variables. Quotes are only required when you are using a variable to create a string that includes the variable contents as a part of the string. In this case the variable will work but the habit will eventually cause you issues.

If you know exactly how long the install takes then you can "sleep" for longer than that to ensure success. This may work but it will not give you accurate or repeatable results.

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

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

Is ideal to use the below
Would this be ideal or should I not go that route?
  1. 'server1', 'server2' | ForEach-Object -Parallel {
  2.  
  3.  
  4.  
  5.     $Software = 'SoftwareName.msi'
  6.  
  7.  
  8.  
  9.     Invoke-Command -ComputerName $_ -ScriptBlock {
  10.  
  11.  
  12.  
  13.         $softwareFormat = "$using:Software"
  14.  
  15.  
  16.  
  17.         $arguments = '/qn /log D:\theLog.log PRODUCT_INSTALLDIR="D:\Program Files\"'
  18.  
  19.  
  20.  
  21.         Start-Process -FilePath $softwareFormat -ArgumentList $arguments -PassThru
  22.  
  23.  
  24.  
  25.     } | Wait-Process
  26.  
  27.  
  28.  
  29. }
  30.  
  31.  

jvierra
Posts: 14672
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 »

No. The process will have exited when the session ends. You must sleep in the session and not after the session.

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

Re: Calling function in a Parallel foreach loop

Post by sekou2331 »

So looks like this works. With the -Wait. Looks like it leaves it in a wait state and comes out.
  1. 'server1', 'server2' | ForEach-Object -Parallel {
  2.  
  3.  
  4.  
  5.     $Software = 'SoftwareName.msi'
  6.  
  7.     Invoke-Command -ComputerName $_ -ScriptBlock {
  8.  
  9.         $softwareFormat = "$using:Software"
  10.  
  11.         $arguments = '/qn /log D:\theLog.log PRODUCT_INSTALLDIR="D:\Program Files\"'
  12.  
  13.         Start-Process -FilePath $softwareFormat -ArgumentList $arguments -PassThru  -Wait
  14.  
  15.     }
  16.  
  17.  
  18.  
  19. }
  20.  

jvierra
Posts: 14672
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 »

WARNING: That will not necessarily work with all MSI installs.

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 then when you say sleep in the session can you give me a example?

jvierra
Posts: 14672
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 »

help start-sleep -online

There are examples in the document.

Locked