Try/catch似乎没有效果

EKS*_*EKS 68 powershell

我是powershell的新手,我试图通过try/catch语句添加错误处理,但它们似乎并没有真正捕获错误.这是powershell v2 CP3.

$objComputer = $objResult.Properties;
$strComputerName = $objComputer.name
write-host "Checking machine: " $strComputerName

try
{
    $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
    foreach ($objItem in $colItems) 
    {
        write-host "Bank Label: " $objItem.BankLabel
        write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
        write-host "Caption: " $objItem.Caption
        write-host "Creation Class Name: " $objItem.CreationClassName      
        write-host
    }
}
Catch 
{
    write-host "Failed to get data from machine (Error:"  $_.Exception.Message ")"
    write-host
}
finally 
{ }  
Run Code Online (Sandbox Code Playgroud)

当它无法联系特定的机器时,我在控制台中得到它,而不是我的干净捕获消息:

Get-WmiObject : The RPC server is
unavailable. (Exception from HRESULT:
0x800706BA) At Z:\7.0 Intern
Programvare\Powershell\Get memory of
all computers in AD.ps1:25 char:34
+ $colItems = get-wmiobject <<<< -class "Win32_PhysicalMemory"
-namespace "root\CIMV2" -computername $strComputerName -Credential
$credentials
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Ste*_*ski 73

尝试运行远程WMI查询时,我能够复制您的结果.抛出的异常不是由Try/Catch捕获的,Trap也不会捕获它,因为它不是"终止错误".在PowerShell中,存在终止错误和非终止错误.似乎Try/Catch/Finally和Trap仅适用于终止错误.

它被记录到$ error自动变量,您可以通过查看$来测试这些类型的非终止错误?自动变量,它会让你知道最后一次操作是成功($ true)还是失败($ false).

从生成的错误的外观,似乎返回错误,而不是包含在可捕获的异常中.下面是生成的错误的痕迹.

PS C:\scripts\PowerShell> Trace-Command -Name errorrecord  -Expression {Get-WmiObject win32_bios -ComputerName HostThatIsNotThere}  -PSHost
DEBUG: InternalCommand Information: 0 :  Constructor Enter Ctor
Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563
DEBUG: InternalCommand Information: 0 :  Constructor Leave Ctor
Microsoft.PowerShell.Commands.GetWmiObjectCommand: 25857563
DEBUG: ErrorRecord Information: 0 :  Constructor Enter Ctor
System.Management.Automation.ErrorRecord: 19621801 exception =
System.Runtime.InteropServices.COMException (0x800706BA): The RPC
server is unavailable. (Exception from HRESULT: 0x800706BA)
   at
System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObjectSearcher.Initialize()
   at System.Management.ManagementObjectSearcher.Get()
   at Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing()
errorId = GetWMICOMException errorCategory = InvalidOperation
targetObject =
DEBUG: ErrorRecord Information: 0 :  Constructor Leave Ctor
System.Management.Automation.ErrorRecord: 19621801
Run Code Online (Sandbox Code Playgroud)

解决代码的方法可能是:

try
{
    $colItems = get-wmiobject -class "Win32_PhysicalMemory" -namespace "root\CIMV2" -computername $strComputerName -Credential $credentials
    if ($?)
    {
      foreach ($objItem in $colItems) 
      {
          write-host "Bank Label: " $objItem.BankLabel
          write-host "Capacity: " ($objItem.Capacity / 1024 / 1024)
          write-host "Caption: " $objItem.Caption
          write-host "Creation Class Name: " $objItem.CreationClassName      
          write-host
      }
    }
    else
    {
       throw $error[0].Exception
    }
Run Code Online (Sandbox Code Playgroud)

  • 您可以通过使用以下命令来抛出非终止错误:-ErrorAction"Stop"(或简称-EA"Stop") (64认同)
  • @JasonMArcher - 对不对!将$ ErrorActionPreference设置为'Stop'也可以,但会产生全局效果. (7认同)
  • 在执行关键行之前,不要忘记清除错误集合.`$ error.Clear()` (3认同)
  • @Steven Murawski-除非您在FInally块中取消设置。 (2认同)
  • @ErnodeWeerd 我不知道为什么人们这么喜欢 `$Error[0]` 。如果您在 catch 中使用 `$_`,则可以避免清除错误缓存以及报告 catch 本身遇到的错误的风险。 (2认同)

Jon*_*lly 60

如果您希望try/catch适用于所有错误(而不仅仅是终止错误),您可以通过设置ErrorActionPreference手动使所有错误终止.

try {

   $ErrorActionPreference = "Stop"; #Make all errors terminating
   get-item filethatdoesntexist; # normally non-terminating
   write-host "You won't hit me";  
} catch{
   Write-Host "Caught the exception";
   Write-Host $Error[0].Exception;
}finally{
   $ErrorActionPreference = "Continue"; #Reset the error action pref to default
}
Run Code Online (Sandbox Code Playgroud)

或者......您可以创建自己的trycatch函数来接受脚本块,这样您的try catch调用就不会像kludge一样.我有我的返回true/false,以防我需要检查是否有错误...但它不必.此外,异常日志记录是可选的,并且可以在catch中处理,但我发现自己总是在catch块中调用日志记录函数,所以我将它添加到try catch函数中.

function log([System.String] $text){write-host $text;}

function logException{
    log "Logging current exception.";
    log $Error[0].Exception;
}


function mytrycatch ([System.Management.Automation.ScriptBlock] $try,
                    [System.Management.Automation.ScriptBlock] $catch,
                    [System.Management.Automation.ScriptBlock]  $finally = $({})){



# Make all errors terminating exceptions.
    $ErrorActionPreference = "Stop";

    # Set the trap
    trap [System.Exception]{
        # Log the exception.
        logException;

        # Execute the catch statement
        & $catch;

        # Execute the finally statement
        & $finally

        # There was an exception, return false
        return $false;
    }

    # Execute the scriptblock
    & $try;

    # Execute the finally statement
    & $finally

    # The following statement was hit.. so there were no errors with the scriptblock
    return $true;
}


#execute your own try catch
mytrycatch {
        gi filethatdoesnotexist; #normally non-terminating
        write-host "You won't hit me."
    } {
        Write-Host "Caught the exception";
    }
Run Code Online (Sandbox Code Playgroud)


小智 15

也可以在单个cmdlet上设置错误操作首选项,而不仅仅是整个脚本.这是使用所有cmdlet上都可用的参数ErrorAction(alisa EA)完成的.

try 
{
 Write-Host $ErrorActionPreference; #Check setting for ErrorAction - the default is normally Continue
 get-item filethatdoesntexist; # Normally generates non-terminating exception so not caught
 write-host "You will hit me as exception from line above is non-terminating";  
 get-item filethatdoesntexist -ErrorAction Stop; #Now ErrorAction parameter with value Stop causes exception to be caught 
 write-host "you won't reach me as exception is now caught";
}
catch
{
 Write-Host "Caught the exception";
 Write-Host $Error[0].Exception;
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*eds 7

这是我的解决方案。当Set-Location失败时,它将引发一个非终止错误,catch块不会看到该错误。添加-ErrorAction Stop是解决此问题的最简单方法。

try {
    Set-Location "$YourPath" -ErrorAction Stop;
} catch {
    Write-Host "Exception has been caught";
}
Run Code Online (Sandbox Code Playgroud)


小智 5

添加“-EA Stop”为我解决了这个问题。