2
$Machines = Get-Content Machines.txt
    foreach($Machine in $Machines)
      {
        $session = New-PSSession -ComputerName $Machine -Credential $creds 
        Invoke-Command -ComputerName $Machine -ScriptBlock {
           #Download 
           & 'wget' 'http://software/software.7z'
           #Extract
           & '7z' 'x' 'C:\software.7z'  '-r' '-y'
           $cleanscript = "Remove-Item C:\software.7z  -Force"
           $cleanscript  | Out-File C:\RemoveZip.ps1 -Encoding ascii
           & "C:\RemoveZip.ps1"
           $script = "CALL C:\software\setup.exe"
           $script  | Out-File C:\software.cmd -Encoding ascii
           & "C:\software.cmd"
       } -Credential $creds 
      }    

After running through 30 machines, PowerShell and the machine running the script runs out of memory. How can this be avoided?

The shell consumes all 16 GBs of memory on running machine. Solutions like exceeding memory for the shell may not work e.g. OutOfMemory Exception on remote execution using Powershell Invoke-Command

1

3 Answers 3

1

If you want to use PowerShell, write PowerShell.

$Machines = Get-Content -Path Machines.txt
ForEach ($Machine in $Machines)
{
    Invoke-Command -ComputerName $Machine -Credential $creds -ScriptBlock {
        #Download 
        Invoke-WebRequest -Uri 'http://software/software.7z' -OutFile 'C:\software.7z'

        #Extract
        Start-Process -FilePath '7z.exe' -ArgumentList 'x','C:\software.7z','-r','-y' -NoNewWindow -Wait
        Remove-Item -Path C:\software.7z -Force

        Start-Process -FilePath 'C:\software\setup.exe'
    }  
}

I suspect all that remote file writing and external calls may have caused problems (also, if you're on v5+, use the *Archive cmdlets over 7z). Additionally, you were generating a session but never using it.

Sign up to request clarification or add additional context in comments.

10 Comments

Start-Process with -Wait is a lot of extra typing when we can use the shorter & operator.
@Bill_Stewart In my experience using &, it has been asynchronous.
Also, why complain about "extra typing" when you're writing a script?
Just pointing out that it's unnecessary. Also, the & operator blocks the script until the command completes (i.e., it's not asynchronous). This is easily demonstrated.
According to your link, the process spawns other tasks and closes the main thread so you end up with an async thing going on. You can mimic what the resolution did with the following: & vs_enterprise.exe --wait --quiet --norestart --installpath C:\VS\2017
|
1

I monitored the memory space for PowerShell during the execution of the script. The Memory instantly starts increasing when running the wget command, the progress output fills the console buffer and eating the PowerShell process memory. I used

        & 'wget' '-q' 'http://software/software.7z' 

which kept memory usage stabilized and also speeded up the execution massively

Hide wget output in Linux

Of course if running PowerShell 5.1 Copy-Files over PSSession can be used instead

Comments

0

Not sure about the memory issues but a few things you could try.

  1. Your New-PsSession doesn't do anything, so either get rid of it, or use it. then you can do invoke-command several times on the same session, instead of sending the big block
  2. Maybe running them in parallel with -nowait, and then wait-job'ing the objects would also help memory issues.
  3. don't foreach, use the pipeline :) $machines | %{ invoke-command -machine $_...

4 Comments

ForEach-Object is slower than ForEach unless you're on PSv5.1
Not sure what the speed of the loop really has to do with it when its wrapping a network call, also do you have a source for that?
Pipeline operations are always slower. Run a test on your own: Measure-Command {ForEach($i in 1..10000){$i}} versus Measure-Command { 1..10000 | % { $_ }}
On average, 6-7x slower.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.