0

我试图在 20 台服务器上并行运行一个相当简单的命令。我到目前为止是这样的:

    $servers = @()
    $servers = (
         "server1",
         "server2",
         "server3",
         "server4",
         "server5",
         "server6"  
    )
    
    $jobs = foreach($server in $servers){
        try{
            $pw = convertto-securestring -AsPlainText -Force -String MyPassword
            $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\Myuser",$pw

            Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-WmiObject -Class Win32_Product | select Name, Version, Vendor | Sort-Object -Property Vendor | where Name -EQ "New Relic Infrastructure Agent" } -AsJob
        }
        catch{
            write-output "ERROR : Failed to connect to server $($server)"
            $_.Exception.Message
            $Error[0].Exception.PSObject.Properties | select Name,Value | FL *
        }    
}

get-job

While ($(Get-Job -State Running).count -gt 0){
    start-sleep 1
}

foreach($job in Get-Job){
    $results= Receive-Job -Id ($job.Id)
}

$results | select PSComputerName, Version | Sort-Object -Property Version | Format-Table -AutoSize

Get-job | Remove-Job -force

更新:我已将上面的代码更新为我目前拥有的代码。现在发生的事情是我正在为一台服务器返回一个结果。其他作业只是被取消或没有完成运行,我不确定。

我已将评论中的建议纳入其中,但看起来我还没有完全到位。有任何想法吗?

4

0 回答 0