首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么-Verbose会禁用$ErrorActionPreference?

为什么-Verbose会禁用$ErrorActionPreference?
EN

Stack Overflow用户
提问于 2016-05-03 22:05:42
回答 3查看 468关注 0票数 9

让我们以这个脚本为例:

代码语言:javascript
复制
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

try
{
  echo "ErrorActionPreference = $ErrorActionPreference"
  Copy-Item  "this_is_a_bad_path" 
  Write-Host "Did not catch error"
}
catch
{
    Write-Host "Caught Error"
}

这与以下输出的预期效果相同:

代码语言:javascript
复制
ErrorActionPreference = Stop
Caught Error

但是,如果我将-verbose添加到行中,并给出Copy-Item -verbose "this_is_a_bad_path",则不再使用$ErrorActionPrefrence,并得到以下输出:

代码语言:javascript
复制
ErrorActionPreference = Stop
Copy-Item : Cannot find path 'C:\Users\porter.bassett\this_is_a_bad_path' because it does not exist.
At C:\Users\porter.bassett\dot.ps1:7 char:3
+   Copy-Item -verbose "this_is_a_bad_path"
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\porter...s_is_a_bad_path:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Did not catch error 

为了让它在-verbose打开、打开的情况下正常工作,我必须将-ErrorAction Stop添加到给出Copy-Item -verbose "this_is_a_bad_path" -ErrorAction Stop的代码行中

为什么在使用-Verbose时不能依赖$ErrorActionPreference

EN

回答 3

Stack Overflow用户

发布于 2016-05-04 01:28:30

这也是Technet上讨论的PowerShell中的一个特性/错误:Verbose common parameter disables ErrorActionPreference

这个特性/错误在PowerShell 4和最新版本5中也存在。

票数 2
EN

Stack Overflow用户

发布于 2017-04-13 19:24:56

好吧,我不知道为什么,但是这个triple face palm问题(好吧,至少是IMHO),在开放源码的PS代码中仍然有效:

https://github.com/PowerShell/PowerShell/blob/02b5f357a20e6dee9f8e60e3adb9025be3c94490/src/System.Management.Automation/engine/MshCommandRuntime.cs#L3207

代码语言:javascript
复制
    /// <summary>
    /// ErrorAction tells the command what to do when an error occurs
    /// </summary>
    /// <exception cref="System.Management.Automation.ExtendedTypeSystemException">
    /// (get-only) An error occurred accessing $ErrorAction.
    /// </exception>
    /// <remarks>
    /// This is a common parameter via class CommonParameters.
    /// </remarks>
    internal ActionPreference ErrorAction
    {
        get
        {
            // Setting CommonParameters.ErrorAction has highest priority
            if (IsErrorActionSet)
                return _errorAction;

            // Debug takes preference over Verbose
            if (Debug)
                return ActionPreference.Inquire;
            if (Verbose)
                return ActionPreference.Continue; *** WTF!?! ***

            // fall back to $ErrorAction
            if (!_isErrorActionPreferenceCached)
            {
                bool defaultUsed = false;
                _errorAction = Context.GetEnumPreference<ActionPreference>(SpecialVariables.ErrorActionPreferenceVarPath, _errorAction, out defaultUsed);
                _isErrorActionPreferenceCached = true;
            }
            return _errorAction;
        }
票数 2
EN

Stack Overflow用户

发布于 2017-01-01 20:45:52

最好的办法是为模块中的每个命令指定erroraction。

write-host foo -ErrorAction Stop

更新:这篇博文很有帮助。它解释了全局var继承如何导致观察到的行为。

https://blogs.technet.microsoft.com/heyscriptingguy/2014/04/26/weekend-scripter-access-powershell-preference-variables/

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37006493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档