Capturing powershell warning

二次信任 提交于 2019-12-06 00:24:01

Given the question's generic title, let me first recap how capturing warnings works in general:

  • To a file such as warnings.txt: with 3> warnings.txt

    • To suppress warnings ad hoc: 3> $null
  • In a variable such as $warnings: with -WarningVariable warnings (-wv warnings)

    • However, that still passes the warnings through and therefore prints them, so in order to prevent that you must also use 3> $null.

Note that these constructs must be applied to the very command producing the warnings, as (by default) only the success output stream (the stream with index 1) is sent through the pipeline:

# OK - saves warnings to file 'warnings.txt' without printing them.
Do-Stuff 3> warnings.txt | Out-File out.txt

# INCORRECT - still prints warnings, because they are NOT sent through the 
#             pipeline, and then creates empty file 'warnings.txt', because
#             Out-File itself produces no warnings.
Do-Stuff | Out-File out.txt 3> warnings.txt

As for what you tried:

There is no output in either output2.txt or warning.txt

  • Presumably, there is no output in output2.txt, because the Remove-MailboxPermission has not produced any success output yet at the time it throws an exception. When the exception occurs, control is instantly transferred to catch handler, so the pipeline is stopped right there, and Out-File never receives input from Remove-MailboxPermission's success stream.

    • Note that try / catch only takes effect if Remove-MailboxPermission produces a statement-terminating error by default; to also make it take effect for non-terminating errors, add -ErrorAction Stop.
      If the Remove-MailboxPermission produces just warnings - and no error at all - then try / catch won't have any effect.
  • There is no output in warning.txt (even with the line re-activated by removing the initial #), because try / catch only catches error output, not warnings; that is, the warnings have already printed by the time the catch handler is processed.

Add -WarningAction Stop or -ErrorAction Stop to your Remove-MailboxPermission command:

try
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Remove-MailboxPermission -Identity "test2" -User "test1" -AccessRights FullAccess -InheritanceType All -confirm:$False -WarningAction Stop -ErrorAction Stop | Out-File c:\temp\output2.txt -Encoding ASCII
}
catch
{
#Write-Warning -Message $($_.Exception.Message) 3 > c:\temp\warning.txt
}

Use the -WarningAction and -WarningVariable common parameters to capture the warning to a variable and cause it to not show to the console.

Remove-MailboxPermission -Identity "test2" -User "test1" -AccessRights FullAccess -InheritanceType All -confirm:$False | Out-File c:\temp\output2.txt -Encoding ASCII -WarningAction SilentlyContinue -WarningVariable CapturedWarning

Then, $CapturedWarning should have the warning in it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!