简体   繁体   English

抑制 cmd 命令上的错误 output Where-Object

[英]Suppress Error output Where-Object on a cmd command

I'm trying to suppress an error output from a Where-Object of a cmd command (gpresult /r), first I saved the output of the gpresult to a variable, then I filtered the variable with the Where-Object and added two filters, to find two AD groups that the user should be member of.我试图从 cmd 命令(gpresult / r)的 Where-Object 中抑制错误 output,首先我保存了 output 的变量,并添加了 gpresult Where-Object 的两个过滤器和过滤器变量, , 以查找用户应属于的两个 AD 组。

The problem comes when the user is not in any of those groups (that it could happen because not everyone uses the programs related to those groups), the console prints a ugly error that we don't want the user to see... I tried adding -ErrorAction SilentlyContinue to the Where-Object with no avail, the error is still popping up.当用户不在任何这些组中时问题就出现了(这可能会发生,因为不是每个人都使用与这些组相关的程序),控制台会打印一个我们不希望用户看到的丑陋错误......我尝试将 -ErrorAction SilentlyContinue 添加到 Where-Object 无济于事,错误仍在弹出。

Do you guys have any clue on this?你们对此有任何线索吗? Here's the code, so you can understand better what I'm trying to suppress:这是代码,因此您可以更好地理解我要抑制的内容:

$gpresult = gpresult /r
$userGroups = ($gpresult | Where-Object -FilterScript {($_ -match 'Group1_*') -or ($_ -match 'Group2_*')} -ErrorAction SilentlyContinue).Trim()

Thanks in advance!提前致谢!

I am not completely sure I understand what you are trying to do but you can separate standard out and standard error streams For example redirecting stderr to null will completely remove it.我不完全确定我理解您要做什么,但您可以将标准输出和标准错误流分开例如将 stderr 重定向到 null 将完全删除它。 If you add this to the end of your command.如果将其添加到命令的末尾。

2>$null

2 is error stream 2 是错误 stream

If you want to separate them later you should be able to.如果您想稍后将它们分开,您应该可以。 Because data from stdout will be strings and data from stderr System.Management.Automation.ErrorRecord objects.因为来自 stdout 的数据将是来自 stderr System.Management.Automation.ErrorRecord 对象的字符串和数据。

$gpresult = gpresult /r
$stderr = $gpresult | ?{ $_ -is [System.Management.Automation.ErrorRecord] }
$stdout = $gpresult | ?{ $_ -isnot [System.Management.Automation.ErrorRecord] }

I tried adding -ErrorAction SilentlyContinue to the Where-Object to no avail, the error is still popping up.我尝试将-ErrorAction SilentlyContinue添加到Where-Object无济于事,错误仍在弹出。

The unwanted error display happens earlier , namely in $gpresult = gpresult /r不需要的错误显示发生得更早,即在$gpresult = gpresult /r

That is, assigning a call to an external program such as gpresult to a PowerShell variable ( $gpresult =... ) captures that program's stdout output in the variable, while passing its stderr output through to the host (console). That is, assigning a call to an external program such as gpresult to a PowerShell variable ( $gpresult =... ) captures that program's stdout output in the variable, while passing its stderr output through to the host (console).

Therefore, to prevent stderr output from printing by simply discarding it, use redirection 2>$null :因此,要通过简单地丢弃stderr output 来防止打印,请使用重定向2>$null

$gpresult = gpresult /r 2>$null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM