1

I'm using Microsoft ExchangePowerShell on an on-premise Exchange (2016 CU19) and I try to catch warning outputs for logging purpose:

  1. get the warning (usually with -WarningVariable)
  2. hide the warning (usually with -WarningAction or redirection 3>$null)

I don't figure out how to do this 2 things at the same time, any clue or explanation would be appreciated.

I use Windows PowerShell 5.1, ExchangePowerShell is installed on an admin server without the full install of Exchange

What I've tried

Using -WarningAction and -WarningVariable

Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningVariable +warnings -WarningAction SilentlyContinue

The warning is hidden but $warnings is empty

Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningVariable +Global:warnings

The warning is displayed and $warnings is set

Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningVariable +warnings

The warning is displayed and $warnings is empty

Wrapping CmdLet with Invoke-Command

Invoke-Command { Set-Mailbox -Identity $identity -PrimarySmtpAddress $address } -WarningVariable +warnings -WarningAction SilentlyContinue

The warning is displayed and $warnings is set

Invoke-Command { Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningVariable +warnings -WarningAction SilentlyContinue }
Invoke-Command { Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningAction SilentlyContinue } -WarningVariable +warnings
Invoke-Command { Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningAction SilentlyContinue } -WarningVariable +Global:warnings

The warning is hidden but $warnings is empty

Invoke-Command { Set-Mailbox -Identity $identity -PrimarySmtpAddress $address -WarningVariable +warnings } -WarningAction SilentlyContinue

The warning is displayed and $warnings is empty

Using redirection

Adding *>$null, to all previous examples, has no effect, the warning is displayed

This is the main incomprehensible point to me. Why, -WarningAction can have an effect on the user interface and the redirection doesn't?

Using preference variables

No effect

Directly using session to push variable preference or override Write-Warning

This is not possible as the remote session is in NoLanguage Mode and only Exchange CmdLet are available.

Using reflection to redirect consolehost output

I don't figure how to do that. This SO answer doesn't help as signatures and type definitions don't match mine.

Gregory_Ott
  • 127
  • 9
  • What's with the `+` sign in front of the warningvariable? `-WarningVariable +warnings` – Theo Oct 18 '21 at 10:41
  • @Theo To append warning to the variable $warnings. See [-WarningVariable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-5.1#-warningvariable) – Gregory_Ott Oct 18 '21 at 11:43
  • Ah, thanks. I have never seen that before. Good to know. – Theo Oct 18 '21 at 12:57

0 Answers0