2

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.

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.

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!

Edit: Here's the error I get (with 2>$null): "Can't call a method in a expression with null value"

2 Answers 2

4

I tried adding -ErrorAction SilentlyContinue to the Where-Object to no avail, the error is still popping up.

The unwanted error happens during the (...).Trim() method call, not in the Where-Object pipeline:

If the pipeline produces no output, the statement is equivalent to $null.Trim(), which predictably causes the following statement-terminating error:

You cannot call a method on a null-valued expression.

Therefore, to avoid this error, you must avoid the .Trim() call if the pipeline produces no output:

$userGroups = 
  $gpresult | 
  Where-Object -FilterScript {($_ -match 'Group1_*') -or ($_ -match 'Group2_*')} |
  ForEach-Object Trim

Note: The above uses simplified syntax to call .Trim() on each input object from the pipeline; if there is no input object, no call is made, which avoids the error.

The non-simplified equivalent of ForEach-Object Trim is ForEach-Object { $_.Trim() }

You could alternatively use a try { ... } catch { ... } statement to suppress the error (a simplified example: try { $null.Trim() } catch { }), but note that catching statement-terminating errors (exceptions) is comparatively slower than the above approach.

Sign up to request clarification or add additional context in comments.

Comments

-1

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. If you add this to the end of your command.

2>$null

2 is error 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.

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

8 Comments

What I'm trying to accomplish here is supressing the error output of the Where-Object when the result of the filter is null. Btw, I tried adding 2>$null to the end of the command but the error still persists
Then my first example should work for you
2>$null is a good pointer (though the problem turned out to be unrelated to stderr output / non-terminating errors). Your seconds snippet would only work if you used $gpresult = gpresult /r 2>&1 as its first statement, because only stdout output is by default captured in variables / sent through the pipeline.
@mklement0 thanks, good to know i am by no means professional in PS but i often use it to make my daily tasks easier
@MichaelFulton, that's a very unhelpful and abrasive comment, anywhere. If you see a technical problem in the comments, point it out. If you personally don't find the style recommendation useful or if you feel that it's fine to let a broken answer stand, as long as the problem is acknowledged in a comment, that is unfortunate, but there's no need to tell anyone, especially devoid of any content other than an insult.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.