代码之家  ›  专栏  ›  技术社区  ›  Ed Mozley

捕获powershell警告

  •  4
  • Ed Mozley  · 技术社区  · 7 年前

    如果我试图从一个邮箱中删除一个没有权限的人的邮箱权限,我试图捕获引发的警告。

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

    两个输出2中都没有输出。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
    }
    
    3 回复  |  直到 7 年前
        1
  •  5
  •   mklement0    7 年前

    鉴于这个问题的一般标题,让我首先回顾一下 捕获警告的工作原理 一般来说 :

    • 到a 文件 例如 warnings.txt :带有 3> warnings.txt

      • 镇压 特别警告: 3> $null
    • 在一个 变量 例如 $warnings :带有 -WarningVariable warnings ( -wv warnings

      • 然而 仍然传递警告 所以打印出来,为了防止这种情况发生,你必须 而且 使用 3>$无效的 .

    注意,这些构造必须应用于产生警告的命令,因为(默认情况下)只有 成功 输出流(具有索引的流 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
    

    至于 您尝试的内容:

    两个输出2中都没有输出。txt或警告。txt文件

    • 据推测,中没有输出 output2.txt ,因为 Remove-MailboxPermission 成功 在抛出异常时输出。当异常发生时,控制权立即转移到 catch 处理程序,所以管道就停在那里 Out-File 从不从接收输入 删除邮箱权限 的成功流。

      • 请注意 try / 接住 仅在以下情况下生效 删除邮箱权限 生成语句- 终止 默认错误;也使其生效 非终止 错误,添加 -ErrorAction Stop .
        如果 删除邮箱权限 生产 只是 警告-并且没有任何错误-然后 尝试 / 接住 不会有任何影响。
    • 中没有输出 warning.txt (即使通过移除初始 # ),因为 尝试 / 接住 仅捕获 错误 输出,非 警告 ; 即警告 已打印 接住 处理程序已处理。

        2
  •  4
  •   Avshalom    7 年前

    添加 -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
    }
    
        3
  •  1
  •   Mike Shepard    7 年前

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