如何添加多线程?

时间:2013-05-03 13:24:31

标签: powershell powershell-v2.0 powershell-v3.0

有没有办法让下面的内容并行运行(多线程)?我有大约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***"
    }
}

1 个答案:

答案 0 :(得分:10)

使用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