powershellexceptiontry-catchwmi

Powershell WMIObject Exception Handling


I am having trouble catching some ManagementExceptions and COMExceptions. these are the two error codes I get, The System.UnauthorizedAccessException seems to be working fine.

    Get-WmiObject :
At C:\Scripts\Exception_CheckDiskSpace.ps1:141 char:26
+             $disks = Get-WmiObject <<<<  -ComputerName $Computer -Class win32
_volume | Where-object {($_.drivetype -eq 2 -or $_.drivetype -eq 3) -and $_.lab
el -ne $null} | Sort-Object -property "name"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand


Get-WmiObject : Access denied
At C:\Scripts\Exception_CheckDiskSpace.ps1:141 char:26
+             $disks = Get-WmiObject <<<<  -ComputerName $Computer -Class win32
_volume | Where-object {($_.drivetype -eq 2 -or $_.drivetype -eq 3) -and $_.lab
el -ne $null} | Sort-Object -property "name"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], Managemen
   tException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
   ommands.GetWmiObjectCommand


Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\Scripts\Exception_CheckDiskSpace.ps1:141 char:26
+             $disks = Get-WmiObject <<<<  -ComputerName $Computer -Class win32
_volume | Where-object {($_.drivetype -eq 2 -or $_.drivetype -eq 3) -and $_.lab
el -ne $null} | Sort-Object -property "name"
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand




Here is the Try Catch I am using (again the System.UnauthorizedAccessException is working fine):

try
        {
            $disks = Get-WmiObject -ComputerName $Computer -Class win32_volume | Where-object {($_.drivetype -eq 2 -or $_.drivetype -eq 3) -and $_.label -ne $null} | Sort-Object -property "name"
        }
        catch [System.UnauthorizedAccessException]
        {
            # Create table data rows 
            $dataRow = "
                <tr>
                    <td width='10%' bgcolor=`'#FF0000`'>$computer</td>
                    <td width='90%' bgcolor=`'#FF0000`' align='center'>$accessDenied</td>
                </tr>
            "
            Add-Content $diskReport $dataRow;
            Write-Host -ForegroundColor DarkRed "Access Denied to $computer";
        }
        catch [System.Management.ManagementException]
        {
            # Create table data rows 
            $dataRow = "
                <tr>
                    <td width='10%' bgcolor=`'#FF0000`'>$computer</td>
                    <td width='90%' bgcolor=`'#FF0000`' align='center'>$accessDenied</td>
                </tr>
            "
            Add-Content $diskReport $dataRow;
            Write-Host -ForegroundColor DarkRed "Access Denied to $computer";
        }
        catch [System.Exception]
        {
            if($_.Exception.GetType() -like "COMException")
            {
                # Create table data rows 
                $dataRow = "
                    <tr>
                        <td width='10%' bgcolor=`'#FF0000`'>$computer</td>
                        <td width='90%' bgcolor=`'#FF0000`'align='center'>$rpcUnavailable</td>
                    </tr>
                "
                Add-Content $diskReport $dataRow;
                Write-Host -ForegroundColor DarkRed "The RPC Server is Unavailable on $computer";
            }
        }

Solution

  • Adding -ErrorAction Stop to the Get-WmiObject per Frode F.'s comment above worked.