0

BasicallyIm trying to run a powershell command from a list of machines.

I can invoke the command on each machine, but i have to wait for each machine to complete the installations before moving on to the next one. When I try creating a job for each machine nothing happens, i suspect its because my script has disconnected from that machines session when it send the command to the next machine.

#Start/Var  
$creds = Get-Credential
$computers = Get-Content -Path .\computers.txt 
$job = start-job -scriptblock { choco install c:\packages.config -y }




foreach($server in $computers) {

CP .\packages.config \\$server\C$ 


Invoke-Command -ComputerName $server -ScriptBlock {$job} -Credential $creds

}
ETHER
  • 37
  • 1
  • 13
  • You ran `choco install` on your computer. So then later on `$Job` isn't a script block, it's the job you ran locally. – BenH Oct 09 '18 at 18:49
  • So should I do Invoke-Command -ComputerName $server -ScriptBlock { start-job -scriptblock { choco install c:\packages.config -y }} -Credential $creds – ETHER Oct 09 '18 at 18:52
  • 1
    My personal preference would be to use the parallelization in `Invoke-Command` and then run `Invoke-Command -ComputerName $servers -ScriptBlock {choco install c:\packages.config -y} -Credential $creds` after the copy loop and skip using Jobs altogether. Otherwise the `Start-Job` would be the "outer" command, and you would invoke against each computer as a separate job. e.g.`start-job -scriptblock { param($server,$creds) Invoke-Command {choco install c:\packages.config -y } -ComputerName $server -Credential $creds} -ArgumentList $server,$creds` but that's messy with the arg list – BenH Oct 09 '18 at 19:03
  • If I invoke-command on a list of computers wouldnt I have to wait for each one to complete before moving on ? Can you show an example of parallzlization for invoke-command please – ETHER Oct 09 '18 at 19:33
  • When using invoke command you should use the parameter `-sessionoption (new-pssessionoption -nomachineprofile)` as it will prevent local profiles from being made saving potentially hours of time. And save data space. – Robert Cotterman Oct 10 '18 at 04:59
  • I'll give that a try, thanks for the suggestion – ETHER Oct 10 '18 at 15:11

1 Answers1

2

As BenH mentioned in the comments you can have Invoke-Command perform things in parallel. The slow down is that you're going to have to wait the files to copy on a per-machine basis, but you can execute the installation on all of the computers at the same time.

#Start/Var  
$creds = Get-Credential
$computers = Get-Content -Path .\computers.txt 

#Copy files to servers one at a time
foreach($server in $computers) {
    CP .\packages.config \\$server\C$ 
}

#Install the package on all servers in parallel 
Invoke-Command -ComputerName $computers -ScriptBlock {choco install c:\packages.config -y} -Credential $creds
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Ah, I see now, since I was invoking the command in the foreach statement it was going one by one. I didn't know Invoke-Command can send out the command to a list of machines all at once. Good to know, thanks everyone – ETHER Oct 10 '18 at 03:47