2014-10-04 89 views
0

我正在写一个PowerShell脚本来帮助加速捕捉管理Windows Update的应用程序中的问题。PowerShell终于尝试catch

我尽可能从数据库中拉出正在管理的机器,我正在处理一个块以处理不同的输出。但我遇到了一个问题,它抛出一个单一的机器2条消息,当我只想要它扔一个,可以用一只手

我意识到它仍然是粗糙的,需要一些清理,我可能只是低咖啡因,但这里是我坚持的块。或者至少在寻找想法。

#Make sure the system is actually on before we try to access the registry and wait for timeout or error 
if (Test-Connection $Device.IPAddresses -Count 1 -ErrorAction SilentlyContinue) 
    { 
    Try { # test the registry connection, this will likely fail on workgroup systems 
     if (Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname) 
      { 
      $SusClientID = $null # blank out the susclientid var so we don't unintentionally recycle it and put out bad data, because purple 
      # now query the registry and put it into a var so we can fookin do stuff 
      $SusClientID = Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname 
      } 
     else {Write-Output $Device.DNSname " is offline"} 
     } # now catch errors, likely caused by credential issues or bad dns resolution 
    Catch [UnauthorizedAccessException] {Write-Output ($Device.DNSname + " cannot access remote registry") 
     } 
    Finally { # finally lets compare the sysclient id on the machine to that in the database 
     if ($SusClientID) # -match "\A[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\z") 
      { 
      if ((Compare-Object $SusClientID $Device.SusClientId) -eq $null) 
       {Write-Output ($Device.DNSname +" SusClientId matches database.")} 
      elseif ($SusClientID -eq $null -or $SusClientID -eq "") {} # this didn't work as I'd hoped 
      else { 
       # notmatching a SusClientId GUID will not catch systems with malformed ID's, commenting this out until a better method is found 
       #if ($SusClientID -notmatch "\A[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\z") 
       Write-Output ($Device.DNSname +" SusClientId does not match.")} 
      } 

     } 
    } 

现在的问题是,在我下面的输出,我赶上了PC-001的异常,并说明其不能被访问,这是我想要的,但后来我也扔由于没有收集到匹配的数据,因此susclientid与数据库中的数据不匹配。在不匹配的消息正在工作,期望为PC-005,但因为我故意替换它的SUSClientID在注册表

pc-001.sergeinc.org cannot access remote registry 
pc-001.sergeinc.org SusClientId does not match. 
pc-002.sergeinc.org SusClientId matches database. 
pc-003.sergeinc.org SusClientId matches database. 
srv-ad.sergeinc.org SusClientId matches database. 
pc-xptest-001.sergeinc.org SusClientId matches database. 
pc-004.sergeinc.org is offline 
pc-005.sergeinc.org SusClientId does not match. 
+2

我认为'$ SusClientID = $ null'应该在Get-RemoteRegistryKey ...之前。 – 2014-10-04 16:37:52

+0

你说得对。那样做了。 – Serge111 2014-10-04 16:59:56

+0

@AlexanderObersht让你的评论成为OP可以接受的答案 – Matt 2014-10-04 18:14:45

回答

0

这可能是个人喜好,但我会尽量把任何必要的几个命令try {...}块。我也不确定是否值得拥有finally {...}区块。

$SusClientID = $null 
... 

try { 
    $SusClientID = Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname 
} 
catch { 
    ... 
} 

if($SusClientID) { 
    ... 
} else { 
    ... 
} 

这可以使它更清楚哪些语句产生了catch {...}块中的错误:在这种情况下,一旦你知道该设备是上线与Test-Connection声明我会做这样的事情。

请注意,如果您通过设备的列表循环,它肯定是工作环境$在每个循环的开始的SUSClientID$空,或者明确地将它设置为$空,如果你发现错误。