我正在编写一个脚本来遍历我域中的所有计算机并显示其名称,描述和上次关机时间。这是一个更好的项目的开始,以更好地跟踪PC维护。到目前为止我的脚本运行得很好:
$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 :(得分:0)
将 -ErrorAction Stop 添加到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