Powershell:如何阻止错误显示在脚本中? [英] Powershell: How can I stop errors from being displayed in a script?
问题描述
由于我的脚本在这样的调用之后检查 $?
的值,并显示和记录错误,所以我宁愿没有几行显示PowerShell错误。
如何停用正在显示的脚本?
你有几个选择。最简单的方法是使用 ErrorAction
设置。
-Erroraction
是所有cmdlet的通用参数。如果有特殊命令要忽略,可以使用 -erroraction'silentlycontinue'
,这将基本上忽略该命令生成的所有错误消息。您还可以使用忽略
值(在PowerShell 3 +中):
与SilentContinue,Ignore不会将错误消息添加到$ Error自动变量。
如果要忽略脚本中的所有错误,您可以使用系统变量 $ ErrorAction
并执行相同的操作: $ ErrorActionPreference ='silentlycontinue'
有关详细信息,请参见 about_CommonParameters 。
When my PowerShell script tries, for example, to create a SQL Server object for a server that doesn't exist ("bla" in my case), PowerShell displays lots of PowerShell errors in red.
Since my script checks the value of $?
after such calls, and displays and logs errors, I'd rather not have the several lines of PowerShell errors displayed as well.
How can I deactivate those being displayed for my script?
You have a couple of options. The easiest involve using the ErrorAction
settings.
-Erroraction
is a universal parameter for all cmdlets. If there are special commands you want to ignore you can use -erroraction 'silentlycontinue'
which will basically ignore all error messages generated by that command. You can also use the Ignore
value (in PowerShell 3+):
Unlike SilentlyContinue, Ignore does not add the error message to the $Error automatic variable.
If you want to ignore all errors in a script, you can use the system variable $ErrorAction
and do the same thing: $ErrorActionPreference= 'silentlycontinue'
See about_CommonParameters for more info.
这篇关于Powershell:如何阻止错误显示在脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!