我的情况:ErrorActionPreference和ErrorAction SilentlyContinue为GET-PSSessionConfiguration
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction SilentlyContinue;
"2 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction Stop;
"3 - $ErrorActionPreference;"
输出:
1 - 停止;
2 - 停止;
并显示错误...
现在,
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
1 - 停止;
并显示错误...
为什么不为GET-PSSessionConfiguration工作-ErrorAction SilentlyContinue)?
更新:
现在,
$ErrorActionPreference = "Continue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
1 - 继续;
2 - 继续;
现在,
$ErrorActionPreference = "SilentlyContinue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
1 - SilentlyContinue;
2 - 默默继续;
的ErrorAction
无处不参数可用于使用参数值SilentlyContinue
沉默非终止错误,它可以用于转换使用参数值Stop
非终止错误终止错误。然而,它无法帮助您忽略终止错误,在这种情况下,Stop-Transcript会抛出终止错误。如果你想忽略,使用一个try/catch例如为:
try { Stop-Transcript } catch {}
我不知道'catch'获得异常仍需要'-ErrorAction Stop':| – AceMark 2015-01-29 02:58:28