8

有没有办法让下面的代码并行运行(多线程)?我有大约 200 台服务器需要运行,并且想知道是否有一种方法可以一次检查 10 台服务器,而不是一次检查一台……WMI 一次检查这台服务器的速度非常慢。

clear
Write-Host "Script to Check if Server is Alive and Simple WMI Check"

$servers = Get-Content -Path c:\Temp\Servers.txt

foreach($server in $servers)
{
    if (Test-Connection -ComputerName $server -Quiet)
    {
    $wmi = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server).Name

    Write-Host "$server responds: WMI reports the name is: $wmi"
    }

    else
    {
    Write-Host "***$server ERROR - Not responding***"
    }
}
4

1 回答 1

11

使用 powershell 作业:

$scriptblock = {
    Param($server)
    IF (Test-Connection $server -Quiet){
        $wmi = (gwmi win32_computersystem -ComputerName $server).Name
        Write-Host "***$server responds: WMI reports the name is: $wmi"
    } ELSE { Write-Host "***$server ERROR -Not responding***" }
}
$servers | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null}
Get-Job | Wait-Job | Receive-Job
于 2013-05-03T13:40:19.010 回答