Попробуйте / поймать, кажется, не имеет эффекта

Я новичок в 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 
{ }  

Когда он не может связаться с конкретной машиной, я получаю это в консоли, а не мое чистое сообщение:

Get-WmiObject : The RPC server is<br> unavailable. (Exception from HRESULT:<br> 0x800706BA) At Z:\7.0 Intern<br> Programvare\Powershell\Get memory of<br> all computers in AD.ps1:25 char:34<br> + $colItems = get-wmiobject <<<< -class "Win32_PhysicalMemory"<br> -namespace "root\CIMV2" -computername $strComputerName -Credential<br> $credentials<br> + CategoryInfo : InvalidOperation: (:) [Get-WmiObject],<br> COMException<br> + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Ответы на вопрос(6)

Ваш ответ на вопрос