gpt4 book ai didi

powershell - 捕获powershell警告

转载 作者:行者123 更新时间:2023-12-04 22:55:51 25 4
gpt4 key购买 nike

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

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

最佳答案

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

  • 到一个文件,例如 warnings.txt :与 3> warnings.txt
  • 禁止临时警告:3> $null
  • 在诸如 $warnings 的变量中:与 -WarningVariable warnings ( -wv warnings )
  • 但是,这仍然会传递警告并因此打印它们,因此为了防止这种情况,您还必须使用 3> $null .

  • 请注意,这些构造必须应用于产生警告的命令,因为(默认情况下)只有成功输出流(索引为 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在它抛出异常时还没有产生任何成功输出。当异常发生时,控制立即转移到catch处理程序,所以管道就停在那里,Out-File从不接收来自 Remove-MailboxPermission 的输入的成功流。
  • 请注意 try/catch仅在 Remove-MailboxPermission 时生效默认情况下会产生语句终止错误;要使其对非终止错误也生效,请添加 -ErrorAction Stop .
    如果Remove-MailboxPermission只产生警告 - 根本没有错误 - 然后 try/catch不会有任何影响。
  • warning.txt中没有输出(即使通过删除初始 # 重新激活该行),因为 try/catch只捕获错误输出,不捕获警告;也就是说,警告已经在 catch 时打印出来了。处理程序被处理。
  • 关于powershell - 捕获powershell警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47376768/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com