2014-05-21 56 views
0

我正在編寫一個腳本來遍歷域中的所有計算機並顯示其名稱,說明和上次關閉時間。這是更好地跟蹤個人電腦維護的更大項目的開始。劇本我到目前爲止工作得很好:在域中的計算機上進行循環,但關閉的計算機除外

$strFilter = "computer" 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain 
$objSearcher.SearchScope = "Subtree" 
$objSearcher.PageSize = 1000 
$objSearcher.Filter = "(objectCategory=$strFilter)" 
$colResults = $objSearcher.FindAll() 
foreach ($i in $colResults) 
{ 
    try{ 
     $wmi = $i.getDirectoryEntry() 
     $lastShutdownTime = get-wmiobject -class Win32_OperatingSystem -computer $wmi.name 
     $Description = Get-WmiObject -Class Win32_OperatingSystem -computer $wmi.name |Select Description 
     write-host $wmi.Name, $Description, $lastShutdownTime.convertToDateTime($lastShutdownTime.lastBootUpTime) 
    } 
    catch [system.exception]{ 

     "Could not reach $wmi.name" 
    } 
} 

除了當我打這臺電腦的時候,我得到忽略我的try/catch塊的錯誤。我該如何編寫它,以便它忽略當前關閉的計算機,或者至少不會出錯?不幸的是,我不能使用AD模塊。

+0

它可能會幫助,如果你嘗試測試連接先看看他們了,你扔在他們一個get-WmiObject可以了。 – mjolinor

回答

0

添加-ErrorAction停止get-wmiobject命令。變化:

$lastShutdownTime = get-wmiobject -class Win32_OperatingSystem -computer $wmi.name 
$Description = Get-WmiObject -Class Win32_OperatingSystem -computer $wmi.name |Select Description 

$lastShutdownTime = get-wmiobject -class Win32_OperatingSystem -computer $wmi.name -ErrorAction Stop 
$Description = Get-WmiObject -Class Win32_OperatingSystem -computer $wmi.name -ErrorAction Stop |Select Description 
相關問題