2

So what I'm trying to do is this

 $corruptAccounts = Get-Mailbox | select-string -pattern WARNING

The intent is to fill the variable $corruptAccounts with the warnings from Get-Mailbox. What actually happens is it processes the Get-Mailbox command, displaying the warnings, and puts nothing into the variable.

I'm new to powershell so I'm still trying to learn some of the basics.

3 Answers 3

5

Try this:

Get-MailBox -WarningVariable wv
$wv

-WarningVariable is a common parameter available for all advanced functions and binary cmdlets.

Here is a generic example:

Function TestWarning {
    [CmdletBinding()]
    param (

    )

    Write-Warning "This is a warning"
}

PS C:\> TestWarning -WarningVariable wv
WARNING: This is a warning

PS C:\> $wv
This is a warning
Sign up to request clarification or add additional context in comments.

1 Comment

I tried Get-Mailbox with the -WarningVariable and it did not put anything into the variable. I tried with wv and $wv
0

You can try this:

[System.Collections.ArrayList]$var = @();
Get-Mailbox -WarningVariable +var -ResultSize unlimited

Comments

0

$buf = Get-Mailbox 2>&1 | Out-String

Will add warnings to $buf at front

Comments

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.