I'm using Microsoft ExchangePowerShell on an on-premise Exchange (2016 CU19) and I try to catch warning outputs for logging purpose:
- get the warning (usually with -WarningVariable)
- 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.