2014-10-04 88 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$空,或者明確地將它設置爲$空,如果你發現錯誤。