【发布时间】: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