ErrorActionPreference和ErrorAction静态地进行Get-PSSessionConfiguration [英] ErrorActionPreference and ErrorAction SilentlyContinue for Get-PSSessionConfiguration

查看:135
本文介绍了ErrorActionPreference和ErrorAction静态地进行Get-PSSessionConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况:

$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction SilentlyContinue;
"2 - $ErrorActionPreference;"
Get-ChildItem NoSuchFile.txt -ErrorAction Stop;
"3 - $ErrorActionPreference;"

输出:


1 - 停止;

2 - 停止;

并显示错误...

1 - Stop;
2 - Stop;
and display an error...

现在,

$ErrorActionPreference = "Stop";
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"

输出:


1 - 停止;

并显示错误...

1 - Stop;
and display an error...

为什么不工作 - 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;"

输出:


1 - 继续;

2 - 继续;

1 - Continue;
2 - Continue;

现在,

$ErrorActionPreference = "SilentlyContinue"
"1 - $ErrorActionPreference;"
(Get-PSSessionConfiguration -Name "MyShellUri" -ErrorAction SilentlyContinue)
"2 - $ErrorActionPreference;"

输出:


1 - SilentlyContinue;

2 - SilentlyContinue;

1 - SilentlyContinue;
2 - SilentlyContinue;

这个参考

可以使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆