Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception Powershell WMIOObject异常处理_Exception_Powershell_Exception Handling_Try Catch_Wmi - Fatal编程技术网

Exception Powershell WMIOObject异常处理

Exception Powershell WMIOObject异常处理,exception,powershell,exception-handling,try-catch,wmi,Exception,Powershell,Exception Handling,Try Catch,Wmi,我很难捕获一些管理异常和ComException。这是我得到的两个错误代码,System.UnauthorizedAccessException似乎工作正常。 获取WmiObject: 在C:\Scripts\Exception\u CheckDiskSpace.ps1:141 char:26 +$disks=Get WmiObject根据Frode F.的上述评论向Get WmiObject添加-ErrorAction Stop有效。您是否尝试将-ErrorAction Stop添加到您的G

我很难捕获一些管理异常和ComException。这是我得到的两个错误代码,System.UnauthorizedAccessException似乎工作正常。

获取WmiObject:
在C:\Scripts\Exception\u CheckDiskSpace.ps1:141 char:26

+$disks=Get WmiObject根据Frode F.的上述评论向Get WmiObject添加-ErrorAction Stop有效。

您是否尝试将
-ErrorAction Stop
添加到您的
Get WmiObject
命令中?您太棒了,尽管我不知道为什么哈哈。谢谢
ErrorAction
参数定义cmdlet(
Get WmiObject
)将如何对错误做出反应。通常,它会给您一个错误,但仍然会继续执行脚本,除非错误是终止(致命)错误。通过将属性设置为
Stop
,您可以告诉它在每次发生错误时抛出异常。
    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
        {
            $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";
            }
        }