我无法捕获一些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";
}
}
答案 0 :(得分:0)
将-ErrorAction Stop添加到Get-WmiObject中,每个Frode F.上面的评论都有效。