Code like this,
try { $NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE" }
catch [GetWMICOMException]
{
"Error 1"
}
catch [System.UnauthorizedAccessException]
{
"Error 2"
}
get an error like:
Can’t find type [GetWMICOMException]:
catch [COMException] Same
catch [System.Runtime.InteropServices.COMException] just ignored
How can I catch it?
Get-WmiObject : RPC server was unavailable. (Exception HRESULT: 0x800706BA)
F:\PowerShell Scripts\Project1.NetReconfigurer\proj1.ps1:36 :33
+ $NIC = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE"
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
First. The error is a non-terminating error, so it just notifies. If you want to catch a non-terminating error, use
-ErrorAction Stop. Also, I don’t thinkCOMExceptionis an exception you can catch. Try this instead: