2012-09-07 45 views
0

我是新来的powershell。我在www.powershell.com上阅读了一些内容。现在我需要你的帮助来解决问题。我想从网络中的客户端读取UUID。因此我创建了一个存放所有PC的文件“pcs.txt”。Powershell超时两秒后

$pc = Get-Content pcs.txt #Read content of file 
$cred = Get-Credential “domain\user” 

for ($i=0; $i -lt $pc.length; $i++)  { 

    $Result=test-connection -ComputerName $pc[$i] -Count 1 -Quiet 
    If ($Result -eq 'True') 
    { 
     $uuid = (Get-WmiObject Win32_ComputerSystemProduct -ComputerName $pc[$i] -Credential $cred).UUID 
     $Ausgabe=$pc[$i] + ';'+$uuid 
     $Ausgabe 


    } 
    else 
    { 

     $Ausgabe=$pc[$i] + '; UUID nicht erhalten' 
     $Ausgabe 
    } 

} 

首先我测试ping是否有效。当ping工作时,我尝试获取uuid。 有时候即使ping工作,我也不会得到uuid。所以我想编一个超时时间,这表示 - >当你在2秒后没有uuid时,进入下一台电脑。

你能帮助我吗?

回答

0

我找到了一个很好的解决方法!

http://theolddogscriptingblog.wordpress.com/2012/05/11/wmi-hangs-and-how-to-avoid-them/

这里我的工作代码:

$pc = Get-Content pcs.txt #FILE FROM THE HARDDISK 
$cred = Get-Credential “DOMAIN\USER” # 

for ($i=0; $i -lt $pc.length; $i++) 
{ 
$Result=test-connection -ComputerName $pc[$i] -Count 1 -Quiet 
If ($Result -eq 'True') 
{ 
    $WMIJob = Get-WmiObject Win32_ComputerSystemProduct -ComputerName $pc[$i] -Credential $cred -AsJob  
    $Timeout=Wait-Job -ID $WMIJob.ID -Timeout 1 # the Job times out after 1 seconds. 
    $uuid = Receive-Job $WMIJob.ID 

    if ($uuid -ne $null) 
    { 
     $Wert =$uuid.UUID 
     $Ausgabe=$pc[$i] + ';'+$Wert 
     $Ausgabe 
    } 

    else 
    { 
    <#$b = $error | select Exception  
    $E = $b -split (:)  
    $x = $E[1] 
    $Error.Clear()  #> 
    $Ausgabe=$pc[$i] + '; got no uuid' 
    $Ausgabe 
    } 



} 
else 
{ 
    $Ausgabe='PC not reached through ping.' 
    $Ausgabe 
} 


} 

我希望我能帮助别人与

0

唉,Get-WmiObject commandlet没有超时参数。 MS Connect有一个功能请求,但它是从2011年开始的。

A workaround,我还没有测试过,可以使用System.Management。我会在这里复制并粘贴它,以防链接失效。 (我讨厌SO答案,只有包含指向可能会或可能不存在resouces ...)

Function Get-WmiCustom([string]$computername,[string]$namespace,[string]$class,[int]$timeout=15){ 
$ConnectionOptions = new-object System.Management.ConnectionOptions 
$EnumerationOptions = new-object System.Management.EnumerationOptions 

$timeoutseconds = new-timespan -seconds $timeout 
$EnumerationOptions.set_timeout($timeoutseconds) 

$assembledpath = "\\" + $computername + "\" + $namespace 
#write-host $assembledpath -foregroundcolor yellow 

$Scope = new-object System.Management.ManagementScope $assembledpath, $ConnectionOptions 
$Scope.Connect() 

$querystring = "SELECT * FROM " + $class 
#write-host $querystring 

$query = new-object System.Management.ObjectQuery $querystring 
$searcher = new-object System.Management.ManagementObjectSearcher 
$searcher.set_options($EnumerationOptions) 
$searcher.Query = $querystring 
$searcher.Scope = $Scope 

trap { $_ } $result = $searcher.get() 

return $result 
} 
+0

的链接是巨大的。但我没有得到它的工作 Alwais它引发了一个异常连接0参数和获得0参数。 $ uuid =(Get-WmiCustom -computername $ pc [$ i] -namespace“root \ cimv2”-class Win32_ComputerSystemProduct -timeout 1).UUID –