问题描述
我的情况:
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction SilentlyContinue;
"2 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction Stop;
"3 - $ErrorActionPreference;"
输出:
现在,
$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
为什么不工作 - GetAPSSessionConfiguration的ErrorAction SilentContinue)
Why doesn't work -ErrorAction SilentlyContinue) for Get-PSSessionConfiguration ?
更新:
/ p>
Now,
$ErrorActionPreference = "Continue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
现在,
$ErrorActionPreference = "SilentlyContinue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"
输出:
这个:
可以使用 ErrorAction
无处不在的参数使用参数值 SilentlyContinue
来静默非终止错误,它可以用于使用参数值 Stop
将非终止错误转换为终止错误。然而,它不能帮助您忽略终止错误,在这种情况下,Stop-Transcript会引发终止错误。如果你想忽略,请使用try / catch,例如:
The ErrorAction
ubiquitous parameter can be used to silence non-terminating errors using the parameter value SilentlyContinue
and it can be used to convert non-terminating errors to terminating errors using the parameter value Stop
. However it can't help you ignore terminating errors and in this case Stop-Transcript is throwing a terminating error. If you want to ignore, use a try/catch e.g.:
try { Stop-Transcript } catch {}
推荐答案
我的解决方案:
$old_ErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
if((Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue) -eq $null) {
WriteTraceForTrans "The session configuration MyShellUri is already unregistered."
}
else {
#Unregister-PSSessionConfiguration -Name "MyShellUri" -Force -ErrorAction Ignore
}
$ErrorActionPreference = $old_ErrorActionPreference
或使用try-catch
Or use try-catch
try {
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
}
catch {
}
这篇关于ErrorActionPreference和ErrorAction静态地进行Get-PSSessionConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!