我们来看看这个剧本:
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"
}
这可以按预期使用此输出:
ErrorActionPreference = Stop
Caught Error
但是,如果我向该行添加-verbose
,给我Copy-Item -verbose "this_is_a_bad_path"
,则不再使用$ErrorActionPrefrence
并获得此输出:
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
使用$ErrorActionPreference
时,为什么我不能依赖-Verbose
?
答案 0 :(得分:1)
这是PowerShell中的一个功能/错误,也在Technet上讨论过:Verbose common parameter disables ErrorActionPreference。
此功能/错误存在于PowerShell 4和最新版本5中。
答案 1 :(得分:1)
好吧,我没有为什么,但这个triple face palm问题(好吧,至少是恕我直言),在开源PS代码中仍然有效:
/// <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 :(得分:-1)
最好的办法是为模块中的每个命令指定错误。
write-host foo -ErrorAction Stop
更新:此博客文章很有帮助。它解释了全局变量继承如何导致观察到的行为。