English 中文(简体)
为什么——停止行动/取消/美元 停止工作,例如隐性重动/Windows PowerShell兼容性特征?
原标题:Why do -ErrorAction Stop / $ErrorActionPreference = Stop not work, such as with implicit remoting / the Windows PowerShell Compatibility feature?
问题回答

两个相关因素决定了<代码>-ErrorAction和/或$ 参考在发出特定指挥时无效:


Workarounds:

  • For commands from regular script modules , i.e. modules implemented in PowerShell code that do not use implicit remoting, which includes CDXML-based modules):

    • 而不是<代码> ErrorAction 缩略语:

      try {     
        Get-NetAdapter nosuch -ErrorAction Stop
      }
      catch {
        "OK, caught: $_"
      }
      
    • Caveats:

      • Unfortunately, these two mechanism aren t created equal: -ErrorAction Stop only acts on non-terminating errors, whereas $ErrorActionPreference = Stop acts on statement-terminating errors too - see this answer. However, in the context of using try / catch, this distinction doesn t matter, because a statement-terminating error by itself also triggers the catch block.

      • 但是,如果目标功能的执行发生,则其他文字模块的指令<>不使用<代码>-ErrorAction,则<>自动生效>。 <>斯特罗内> 参考全球确保可预见的行为——见下点。

  • For commands from modules that use implicit remoting, which includes those loaded via the Windows PowerShell Compatibility feature (and which are by definition also script modules):

    • <>strong>(当时)将global$ErrorAction> 缩略语改为<条码>

      & { # Simulate running in a child scope.
        $prevGlobalPref = $global:ErrorActionPreference
        $global:ErrorActionPreference =  Stop 
        try {     
          Get-AppLockerFileInformation NoSuch.exe
        }
        catch {
          "OK, now we get here: $_"
        } finally {
          $global:ErrorActionPreference = $prevGlobalPref
        }
      }
      

As for how to tell whether a given command is from either module type:

  • The following helper function, Get-CommandModuleType, tells you what type of module a given command is from, with output Script indicating a regular (non-implicitly-remoting) script module, and ImplicitRemoting an implicitly remoting one; see the comment-based help in the source code for details.

  • Sample calls, from PowerShell (Core) on Windows:

    # ->  Script   
    Get-CommandModuleType.ps1 Get-NetAdapter 
    
    # ->  ImplicitRemoting 
    Get-CommandModuleType.ps1 Get-AppLockerFileInformation 
    
function Get-CommandModuleType {
  <#
.SYNOPSIS
  Indicates the type of module that the specified command is from.
.DESCRIPTION
  Aliases are automatically resolved to their underlying commands.

  Note:
   * If the command *isn t from a module*, there is no output.
   * The output is a *string* that is one of the following:
     *  ImplicitRemoting  for an implicitly remoting module.
     *  Script  for regular script modules (that don t use implicit remoting)
     * Otherwise, the stringified version of the System.Management.Automation.ModuleType
       value reported by the `.ModuleType` property of the underlying module.
   * IMPORTANT:
     * Not all script modules report themselves as such via the underlying
       module s manifest; some of them report just  Manifest . If you want
       to see this formal value, use the -Raw switch.
  
.EXAMPLE
  Get-CommandModuleType Get-NetAdapter

  Reports the *de facto* type of module that hosts the Get-NetAdapter command.
.EXAMPLE
  Get-CommandModuleType Get-NetAdapter -Raw 

  Reports the *formal* type of module that hosts the Get-NetAdapter, as implied
  by its manifest.
#>

  [CmdletBinding()]
  param(
    [Parameter(Mandatory)]
    [string] $CommandName,
    [switch] $Raw
  )

  $command = Get-Command $CommandName
  if ($command -and $command.ResolvedCommand) { 
    $command = $command.ResolvedCommand
    Write-Verbose "Resolved command is: $command"
  }
  if ($module = $command.Module) {
    Write-Verbose "Source module is: $module"
    if (-not $Raw -and $command.CommandType -eq  Function  -and $module) {
      # a script module
      if ($module.PrivateData.ImplicitRemoting) {
        # an implicitly remoting module
         ImplicitRemoting 
      }
      else {
         Script 
      }
    }
    else {
      if ($Raw) { Write-Verbose  Reporting raw module type, i.e as manifested.  }
      $module.ModuleType.ToString()
    }
  }
}




相关问题
Separating Business Layer Errors from API errors

The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

How to tell why a file deletion fails in Java?

File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...