【问题标题】:Capturing powershell warning捕获 powershell 警告
【发布时间】:2018-05-02 18:32:05
【问题描述】:

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

#$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
}

【问题讨论】:

  • 2&gt;c:\temp\errors.txt -> $_ &gt; c:\temp\errors.txt - $_ 代表手头的异常。 (2&gt;c:\temp\errors.txt 所做的是通过成功输出流向文件发送整数 2(字符串化)。

标签: powershell warnings exchange-server


【解决方案1】:

使用 -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 中应该有警告。

【讨论】:

  • 我仍然收到黄色警告,但是当我尝试显示 CapturedWarning 变量的内容时,它是空的。
  • 回显“警告:$CapturedWarning”
  • 奇怪。我无法测试您的特定 cmdlet,但创建一个仅写入警告的“测试”函数表明该技术工作正常,因为它在变量中捕获警告并且不会将其写入控制台。
【解决方案2】:

-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
}

【讨论】:

  • -ErrorAction Stop 在这种情况下是拼图的重要组成部分。在这种情况下,如果有警告,我可以继续进行,因为警告并不严重 :) 谢谢!
  • 对于那些实际尝试处理从各种命令(例如 IISAdministration 或 WebAdministration 模块中的命令)返回的“警告”的人,-WarningAction 是解决方案。因此 +1
【解决方案3】:

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

  • 到一个文件 比如warnings.txt: 和3&gt; warnings.txt

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

    • 但是,仍然通过警告并因此打印它们,所以为了防止您必须使用3&gt; $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

至于你尝试了什么:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多