Powershell WMIObject 异常处理

问题描述 投票:0回答:2

我在捕获某些 ManagementException 和 COMException 时遇到问题。这是我得到的两个错误代码,System.UnauthorizedAccessException 似乎工作正常。

    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




这是我正在使用的 Try Catch(System.UnauthorizedAccessException 再次工作正常):

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";
            }
        }
powershell exception try-catch wmi
2个回答
0
投票

根据上面的 Frode F. 评论,将 -ErrorAction Stop 添加到 Get-WmiObject 中。


0
投票

WMI 查询后使用 -ErrorAction Stop。

示例 Get-WmiObject -ComputerName $computer Win32_Product -ErrorAction Stop | Where-Object {$_.Name -like "Forticlient"}

© www.soinside.com 2019 - 2024. All rights reserved.