0

编写了一个小脚本,使用 WMI 查询从 windows 服务器中查找多路径数。它适用于可以直接连接而没有任何问题的服务器。但是,如果一台服务器可以 ping,但无法通过 WMI 脚本访问,则返回错误需要很长时间(例如,如果 server.txt 列表中存在 linux 服务器主机名).. 有人可以帮我做同样的事情吗以更快的方式..?

$Servers = Get-Content .\Servers.txt

$ErrorActionPreference = ‘SilentlyContinue’

FOREACH ($Server in $Servers) {

Write-Host $Server -nonewline

if (test-connection -computername $Server -Count 1 -quiet) {

$Name = $null
$NoPath =$null
$MPIODisks =$null

$MPIODisks = Get-WmiObject -Namespace root\wmi -Class mpio_disk_info -ComputerName "$Server" |Select-Object "DriveInfo"

    if ($MPIODisks -eq $Null) {

    write-host "`t - Unable to connect" -fore "RED"

    } else {    

        write-host ""
        write-host "Drive Name `tNo.Path" -fore "yellow"

            Foreach ($Disk in $MPIODisks) {
                $mpiodrives = $disk.DriveInfo

                    foreach ($Drive in $mpiodrives) {
                $Name = $Drive.Name
                $NoPath = $Drive.Numberpaths

                    If ($NoPath -lt 4) {
                    Write-Host $Name `t -nonewline
                    write-host $NoPath -fore "Red"
                    } else {
                    Write-Host $Name `t -nonewline
                    write-host $NoPath -fore "Green"
                    }
                    }
            }

    }

    write-host ""

} else {

write-host "`t- Unknown Host" -fore "Red"
write-host ""
}

}

4

2 回答 2

4

有一个用于添加超时参数的连接项Get-WmiObject。该项目中提到的解决方法是将您的 WMI 命令通过管道传输到Wait-Job并指定超时时间(以秒为单位)。

只要您在 PS 版本 3.0 或更高版本上,这应该适合您:

Get-WmiObject win32_computersystem -ComputerName <hostname> -AsJob | Wait-Job -Timeout 10 | Receive-Job
于 2016-04-24T07:05:33.570 回答
1

作为替代方案,您可以一次向所有服务器询问结果,方法是将它们全部传递到查询中,并避免一次查询一个服务器的慢循环。我没有任何 MPIO 驱动器可供测试,但它可能看起来像这样(使用Get-Ciminstance它需要一个超时参数):

$servers = Get-Content .\Servers.txt

# Get data from all servers with timeout
$servers_ok = Get-CimInstance -computername $servers -Namespace root\wmi -Class mpio_disk_info -ErrorAction SilentlyContinue -OperationTimeoutSec 1 | group pscomputername

# Output which servers gave no result back
foreach($no_result in $($servers | where { $_ -NotIn $servers_ok.Name })) {
    write-host "No result for $no_result" -ForegroundColor Red
}

# Loop over the results and output
foreach($server in $servers_ok) {

    Write-Host $server.Name 

    foreach($mpiodisk in $server.group)  {

        $mpiodrives = $mpiodisk.DriveInfo

        foreach ($mpiodrive in $mpiodrives) {

            $name = $mpiodrive.Name
            $noPath = $mpiodrive.NumberPaths

            If ($NoPath -lt 4) {
                write-host $name `t -nonewline
                write-host $noPath -fore "Red"
            } else {
                write-host $name `t -nonewline
                write-host $noPath -fore "Green"
            }
        }
    }
}
于 2016-04-24T14:00:24.313 回答