0

このようなコード、

try { $NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE" }

catch [GetWMICOMException]
{
    "Error 1"
}
catch [System.UnauthorizedAccessException]
{
    "Error 2"
}

次のようなエラーが発生します。

タイプが見つかりません [GetWMICOMException]:

catch [COMException] 同上

catch [System.Runtime.InteropServices.COMException] は無視されました

どうすれば捕まえられますか?

Get-WmiObject : RPC server was unavailable. (Exception HRESULT: 0x800706BA)
F:\PowerShell Scripts\Project1.NetReconfigurer\proj1.ps1:36 :33
+             $NIC = Get-WmiObject <<<<  Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE"

+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
4

4 に答える 4

2

初め。エラーは非終了エラーなので、通知するだけです。終了しないエラーをキャッチする場合は、 を使用します-ErrorAction StopCOMExceptionまた、キャッチできる例外はないと思います。代わりにこれを試してください:

try { $NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE -ErrorAction Stop}
catch [System.UnauthorizedAccessException]
{
    "Error 2"
}
catch [Exception]
{
    if ($_.Exception.GetType().Name -eq "COMException") {
        "Error 1"
    }
}
于 2013-01-30T08:32:40.353 に答える
1

終了エラーのみをキャッチできます。'-ErrorAction Stop' を Get-WmiObject に追加して、エラーを終了エラーに変換し、再試行してください。余談ですが、ping 要求を使用してターゲット システムに対して wmi クエリを実行する前に、ターゲット システムへの接続をテストすることをお勧めします。これにより、特に多数のコンピューターにクエリを実行する場合に実行速度が向上します (wmi タイムアウトによりスクリプトが遅くなる可能性があります)。

try { 
    $NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computername -Credential $Credential -filter "IPEnabled = $TRUE" 
}
catch
{
    $_
}
于 2013-01-30T08:33:46.093 に答える
0

Try{}Catch と -ErrorVariable を使用して、ErrorAction モードの「SilentlyContinue」であらゆる種類のエラーを処理できました。{}

    foreach ($Server in $Server_List)
    {
        $ErrorVar = $null
        $Server_Model = $null

        try
        {
            $Server_Info= Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Server -Credential $Credential -ErrorAction SilentlyContinue -ErrorVariable ErrorVar | Select Model
        }
        catch [System.UnauthorizedAccessException] # here $ErrorVar.Exception doesn't exist but $ErrorVar.ErrorRecord.Exception
        {
            $Msg = "Powershell cmdlet on $Server : DCOM unauthorized access"
            $Msg | Write-Warning
        }

        if ($ErrorVar.Exception)
        {
            switch ($ErrorVar)
            {
                { $_.Exception.GetType().Name -eq "COMException" }
                {
                    $Msg = "Powershell cmdlet on $Server : RPC server is unavailable"
                    $Msg | Write-Warning

                    break
                }
                { $_.Exception.GetType().Name -eq "ManagementException" }
                {
                    $Msg = "Powershell cmdlet on $Server : user credentials cannot be used for local connections.`nRetrying without credential."
                    $Msg | Write-Warning
                    $Server_Info = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Server | Select Model # when the script is hosted on a computer within the Server_List

                    break
                }
                default
                {
                    $Msg = "Powershell cmdlet on $Server : unexpected error"
                    $Msg | Write-Warning
                }
            }
        }

        if ($Server_Info)
        {
                $Server_Info_Pso =  New-Object PSObject -Property @{
                    HostName = $Server
                    Model = $Server_Info.Model
                }
        }
        else
        {           
            $Server_Info_Pso =  New-Object PSObject -Property @{
                HostName = $Server
                Model = 'Unavailable'
            }
        }

        $Server_Info_PsoCol = $Server_Info_PsoCol + @($Server_Info_Pso)
    }
于 2014-07-05T23:53:20.197 に答える