Try / catch não parece ter efeito
Eu sou novo no powershell, e estou tentando adicionar tratamento de erros através de instruções try / catch, mas eles não parecem estar realmente pegando o erro. Este é o 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
{ }
Quando não consigo entrar em contato com uma máquina específica, recebo isso no console e não na minha mensagem de captura limpa:
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