【问题标题】:Powershell WMIObject Exception HandlingPowershell WMIObject 异常处理
【发布时间】:2014-02-28 18:44:19
【问题描述】:

我无法捕获一些 ManagementExceptions 和 COMExceptions。这些是我得到的两个错误代码, 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";
            }
        }

【问题讨论】:

  • 您是否尝试将-ErrorAction Stop 添加到您的Get-WmiObject 命令中?
  • 你太棒了,虽然我不知道为什么哈哈。谢谢!

标签: exception powershell exception-handling try-catch wmi


【解决方案1】:

根据 Frode F. 的上述评论,将 -ErrorAction Stop 添加到 Get-WmiObject 是有效的。

【讨论】:

  • ErrorAction 参数定义 cmdlet (Get-WmiObject) 对错误的反应方式。通常它会给你一个错误,但除非错误是终止(致命)错误,否则仍会继续执行脚本。通过将属性设置为Stop,您可以告诉它每次发生错误时抛出异常。
猜你喜欢
  • 2013-05-30
  • 1970-01-01
  • 2019-04-12
  • 2021-12-20
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 2014-04-06
  • 2021-10-30
相关资源
最近更新 更多