捕获powershell警告

捕获powershell警告,powershell,warnings,exchange-server,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

我正在尝试捕获如果我尝试从邮箱中删除某个首先没有权限的人的邮箱权限时引发的警告

#$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
{
$_ > 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
添加到
删除邮箱权限
命令:

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
    :带有
    -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

至于你尝试了什么:

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

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

    • 请注意,
      try
      /
      catch
      仅在
      Remove MailboxPermission
      默认生成语句终止错误时生效;要使其对非终止错误生效,请添加
      -ErrorAction Stop

      如果
      Remove mailbox权限
      只产生警告,而没有任何错误,那么
      try
      /
      catch
      将不会产生任何效果
  • warning.txt
    中没有输出(即使通过删除初始的
    #
    重新激活行),因为
    try
    /
    catch
    只捕获错误输出,而不捕获警告;也就是说,在处理
    catch
    处理程序时,警告已经打印出来


我仍然收到黄色警告,但当我尝试显示CapturedWarning变量的内容时,它是空的。echo“warning:$CapturedWarning”奇怪。我无法测试您的特定cmdlet,但创建一个只写入警告的“测试”函数表明此技术工作良好,因为它将警告捕获在变量中,而不会写入控制台。
2>c:\temp\errors.txt
->
$\c:\temp\errors.txt
-
$
表示当前的异常。(
2>c:\temp\errors.txt
所做的是通过成功输出流将integer
2
(字符串化)发送到文件中)。-在这种情况下,ErrorAction Stop是拼图的一个重要部分。在这种情况下,如果有警告,我可以继续,因为警告并不严重:)谢谢!对于那些实际试图处理从各种命令(如IISAdministration或WebAdministration模块中的命令)返回的“警告”的用户,WarningAction是解决方案。因此+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