我是powershell的新手,我试图通过try/catch语句添加错误处理,但它们似乎并没有真正捕获错误.这是powershell v2 CP3.
$objComputer = $objResult.Properties;
$strComputerName = $objComputer.name
write-host "Checking machine: " $strComputerName
try
{
$colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
foreach ($objItem in $colItems)
{
write-host "Bank Label: " $objItem.BankLabel
write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
write-host "Caption: " $objItem.Caption
write-host "Creation Class Name: " $objItem.CreationClassName
write-host
}
}
Catch
{
write-host "Failed to get data from machine (Error:" $_.Exception.Message ")"
write-host
}
finally
{ }
Run Code Online (Sandbox Code Playgroud)
当它无法联系特定的机器时,我在控制台中得到它,而不是我的干净捕获消息:
Get-WmiObject : The RPC …
powershell ×1