[英]How do I prevent an error message when using get-process for an process that does not run?
When I execute当我执行
get-process noSuchProcess
I get the error message我收到错误消息
get-process : Cannot find a process with the name "noSuchProcess". Verify the process name and call the cmdlet again.
At line:1 char:5
+ get-process noSuchProcess
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (noSuchProcess:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
Is it possible to prevent such an error message if a process does not exist?如果进程不存在,是否可以防止此类错误消息?
To complement Yosev Arbiv's helpful answer with PowerShell-idiomatic alternatives, using the为了用 PowerShell 惯用的替代方法补充Yosev Arbiv 的有用答案,使用
-ErrorAction
( -ea
) common parameter : -ErrorAction
( -ea
) 常用参数:
# Ignores the error altogether.
# Note the use of the -ea alias for -ErrorAction
Get-Process noSuchProcess -ea Ignore
# Silences the error, but still records it in $Error (see next section)
Get-Process noSuchProcess -ea SilentlyContinue
# If you call an *external program*, you must use 2>$null to silence
# its stderr output.
whoami badarg 2>$null
Note:笔记:
-ErrorAction
(and -ErrorVariable
, see below) can only be used with PowerShell cmdlets and advanced functions or scripts, not with external programs. -ErrorAction
(和-ErrorVariable
,见下文)只能与 PowerShell cmdlet 和高级函数或脚本一起使用,而不能与外部程序一起使用。
To silence stderr output from external programs, use 2>$null
.要使外部程序的 stderr output 静音,请使用
2>$null
。
2>$null
is virtually equivalent to -ErrorAction SilentlyContinue
, except for the situationally different behavior introduced by the bug reported in this GitHub issue . 2>$null
实际上等同于-ErrorAction SilentlyContinue
,除了此 GitHub 问题中报告的错误引入的情况不同的行为。 Both -ErrorAction
and 2>
by default only act on non-terminating errors (the typical kind), not terminating ones, though, confusingly, the seemingly equivalent $ErrorActionPreference
preference variable acts on all errors.默认情况下
-ErrorAction
和2>
都只作用于非终止错误(典型类型),而不是终止错误,但令人困惑的是,看似等效的$ErrorActionPreference
首选项变量作用于所有错误。
Background information:背景资料:
Note: The term cmdlets below is used to refer not just to true (compiled) cmdlets, but also to their (written-in-PowerShell) counterparts, advanced functions and scripts .注意:下面的术语cmdlet不仅指真正的(编译的)cmdlet,还指它们的(在 PowerShell 中编写的)对应物、 高级函数和脚本。
PowerShell's analog to stderr (the standard error stream) is the error [output] stream , which is one of 6 available streams, documented in about_Redirection . PowerShell 对 stderr(标准错误流)的模拟是错误 [输出] stream ,它是about_Redirection中记录的6 个可用流之一。
By default, all errors that occur in a session are recorded in the automatic $Error
variable , in reverse chronological order (most recent one first) - whether they are printed to the console at the time they occur or not.默认情况下,session 中发生的所有错误都按时间倒序(最近的一个在前)记录在自动
$Error
变量中 - 无论它们在发生时是否打印到控制台。
As an alternative to examining $Error
, the common -ErrorVariable
( -ev
) parameter allows you to capture a command's errors in a variable;作为检查
$Error
的替代方法, 常见的-ErrorVariable
( -ev
) 参数允许您在变量中捕获命令的错误; eg:例如:
# Silence any errors, but capture them in variable $err Get-Process noSuchProcess -ea SilentlyContinue -ev err
There is no equivalent mechanism when you call external programs, though one has been proposed in this GitHub feature request .调用外部程序时没有等效机制,尽管在此 GitHub 功能请求中提出了一种机制。
Using -ErrorAction
in a cmdlet call:在 cmdlet 调用中使用
-ErrorAction
:
-ErrorAction SilentlyContinue
silences (doesn't print to the console) any error output, but still records it in $Error
. -ErrorAction SilentlyContinue
(不打印到控制台)任何错误 output,但仍将其记录在$Error
中。
-ErrorAction Ignore
both silences and suppresses addition to $Error
-ErrorAction Ignore
沉默并抑制对$Error
的添加
Stderr output from external programs is not recorded in $Error
by default (stderr output cannot generally assumed to represent errors , given that many programs, such as git
, also use it to print status information).外部程序的stderr output默认不记录在
$Error
中(stderr output一般不能假设代表错误,因为很多程序,比如git
,也用它来打印状态信息)。
2>$null
does cause the stderr lines to be recorded in $Error
;2>$null
确实会导致 stderr 行记录在$Error
中; this unexpected behavior is discussed in this GitHub issue .
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.