捕获powershell警告

Ed *_*ley 4 powershell warnings exchange-server

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

#$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
}
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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

$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
}
Run Code Online (Sandbox Code Playgroud)

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

$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
}
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 5

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

  • 到一个文件,如warnings.txt:与3> warnings.txt

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

    • 但是,这仍然会传递警告并因此打印它们,因此为了防止这种情况,您必须使用3> $null.

请注意,这些构造必须应用于产生警告的命令,因为(默认情况下)只有成功输出流(带有 index 的流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
Run Code Online (Sandbox Code Playgroud)

至于你尝试什么:

output2.txt 或 warning.txt 中都没有输出

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

    • 请注意,try/catch仅在默认情况下Remove-MailboxPermission产生语句终止错误时生效;要使其对非终止错误也生效,请添加-ErrorAction Stop.
      如果Remove-MailboxPermission产生警告 - 而根本没有错误 - 那么/将不会产生任何影响。trycatch
  • 没有输出warning.txt(即使通过删除初始重新激活该行#),因为try/catch只捕获错误输出,而不是警告;也就是说,警告已经catch处理处理程序时打印出来了。