Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Run invoke-command on multiple machines at the same time

$
0
0

Hey all so I read that if I store my New-pssession in a variable then used that in my invoke-command it would run all computers at the same time.

$a = Get-Content "C:\Users\cody-horton\Desktop\list.txt"
$session

for($i=0;$i -lt $a.Length;$i++){

    if(!(Test-Connection -Cn $a[$i] -BufferSize 16 -Count 1 -ea 0 -quiet)){
    
        Write-Host $a[$i] -foregroundcolor red

    }
    else{

        $session = New-PSSession $a[$i]

}
}

Invoke-Command -Session $session -FilePath "\\My computer\C`$\Users\public\Documents\zip folder.ps1"

What exactly am I doing wrong I just need to run this script on multiple machines at the same time.

Thanks.

Edit: Also what would be the best way to close all the sessions thanks.

Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>