捕获 powershell 警告

Capturing powershell warning

我正在尝试捕获如果我尝试从一开始就没有权限的人的邮箱中删除邮箱权限时抛出的警告。

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

output2.txt 或 warning.txt 都没有输出 - 我做错了什么?

我试图捕获的警告以黄色显示并表示:

WARNING: The cmdlet extension agent with the index 0 has thrown an exception in OnComplete(). The exception is:
System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Microsoft.Exchange.Data.Storage.ExchangePrincipal.get_ServerFullyQualifiedDomainName()
   at Microsoft.Exchange.Data.Storage.MailboxSession.Initialize(MapiStore linkedStore, LogonType logonType,
ExchangePrincipal owner, DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags,
GenericIdentity auxiliaryIdentity)
   at Microsoft.Exchange.Data.Storage.MailboxSession.<>c__DisplayClass12.<CreateMailboxSession>b__10(MailboxSession
mailboxSession)
   at Microsoft.Exchange.Data.Storage.MailboxSession.InternalCreateMailboxSession(LogonType logonType,
ExchangePrincipal owner, CultureInfo cultureInfo, String clientInfoString, IAccountingObject budget, Action`1
initializeMailboxSession, InitializeMailboxSessionFailure initializeMailboxSessionFailure)
   at Microsoft.Exchange.Data.Storage.MailboxSession.ConfigurableOpen(ExchangePrincipal mailbox, MailboxAccessInfo
accessInfo, CultureInfo cultureInfo, String clientInfoString, LogonType logonType, PropertyDefinition[]
mailboxProperties, InitializationFlags initFlags, IList`1 foldersToInit, IAccountingObject budget)
   at Microsoft.Exchange.Data.Storage.MailboxSession.OpenAsSystemService(ExchangePrincipal mailboxOwner, CultureInfo
cultureInfo, String clientInfoString)
   at Microsoft.Exchange.ProvisioningAgent.MailboxLoggerFactory.XsoMailer.Log(AdminLogMessageData data,
LogMessageDelegate logMessage)
   at Microsoft.Exchange.ProvisioningAgent.AdminLogProvisioningHandler.OnComplete(Boolean succeeded, Exception e)
   at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)
WARNING: Can't remove the access control entry on the object "CN=xxxxx" for account "xxxxx" because the ACE doesn't exist on the
object.

感谢迄今为止的所有帮助!我的代码现在看起来像这样:

$WarningPreference = "continue"
try
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Remove-MailboxPermission -Identity "test1" -User "test2" -AccessRights FullAccess -InheritanceType All -confirm:$False 2> c:\temp\errors.txt 3> c:\temp\warnings.txt -ErrorAction Stop
}
catch
{
2> c:\temp\errors.txt
}

*最终解决方案 - 谢谢大家 *

$WarningPreference = "continue"
try
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Remove-MailboxPermission -Identity "test1" -User test2" -AccessRights FullAccess -InheritanceType All -confirm:$False 2> c:\temp\errors.txt 3> c:\temp\warnings.txt -ErrorAction Stop
}
catch
{
$_ > c:\temp\errors.txt
}

使用 -WarningAction 和 -WarningVariable 通用参数将警告捕获到变量并使其不显示到控制台。

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

那么,$CapturedWarning里面应该有警告。

-WarningAction Stop-ErrorAction Stop 添加到您的 Remove-MailboxPermission 命令:

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
}

考虑到问题的通用标题,让我首先回顾一下捕获警告的工作原理一般:

  • 文件,例如warnings.txt:用3> warnings.txt

    • 抑制 临时警告:3> $null
  • 在一个变量中,例如$warnings:with -WarningVariable warnings (-wv warnings)

    • 然而,仍然通过传递警告并因此打印它们,所以为了防止你必须使用3> $null.

请注意,这些结构必须应用于产生警告的命令,因为(默认情况下)只有 success 输出流(索引为 1 的流) 通过管道发送:

# 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

至于你试过的:

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

  • 大概output2.txt没有输出,因为Remove-MailboxPermission当时还没有产生任何success输出它抛出异常。当异常发生时,控制权立即转移到 catch 处理程序,因此管道就在那里停止,并且 Out-File 永远不会收到来自 Remove-MailboxPermission 的成功流的输入。

    • 注意,try / catch 仅在 Remove-MailboxPermission 产生 statement-terminating 默认错误时生效;要使其对 non-terminating 错误也生效,请添加 -ErrorAction Stop.
      如果 Remove-MailboxPermission 产生 just 警告 - 并且根本没有错误 - 那么 try / catch 将没有任何效果。
  • warning.txt中没有输出(即使通过删除初始#行re-activated),因为try / catch 只捕获 error 输出,不捕获 warnings;也就是说,在处理 catch 处理程序时,警告 已经打印