2016-04-24 50 views
1

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

$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 "" 
} 

}

+0

哪个版本的PowerShell您使用的是您的客户端和服务器上? –

+0

在新版本上,您可以使用'Get-CimInstance -OperationTimeoutSec' –

回答

3

a connect item for Get-WmiObject to add a timeout parameter。该项目中提到的一种解决方法是仅将您的WMI命令传输到Wait-Job并指定以秒为单位的超时期限。

只要你在PS 3.0或更高版本,这应该为你工作:

Get-WmiObject win32_computersystem -ComputerName <hostname> -AsJob | Wait-Job -Timeout 10 | Receive-Job 
2

作为替代方案,你可以通过他们都通过到查询要求所有服务器的结果在一次避免一次慢查询一台服务器。我没有任何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" 
      } 
     } 
    } 
} 
相关问题