Try / catch scheint keine Wirkung zu haben

Ich bin neu in Powershell, und ich versuche, eine Fehlerbehandlung über try / catch-Anweisungen hinzuzufügen, aber sie scheinen den Fehler tatsächlich nicht abzufangen. Dies ist 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 
{ }  

Wenn die Verbindung zu einem bestimmten Computer nicht hergestellt werden kann, wird dies in der Konsole und nicht in der Meldung "Clean Catch" angezeigt:

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

Antworten auf die Frage(6)

Ihre Antwort auf die Frage