Try / catch nie działa
Jestem nowym użytkownikiem PowerShell i próbuję dodać obsługę błędów za pomocą instrukcji try / catch, ale wydaje się, że nie łapią one błędu. To jest powerhell 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
{ }
Kiedy nie udaje się skontaktować z konkretną maszyną, dostaję to w konsoli, a nie w moim czystym komunikacie:
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