捕获powershell警告

时间:2017-11-19 12:20:16

标签: 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
}

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
}

3 个答案:

答案 0 :(得分:3)

<?php // Register the script wp_register_script( 'some_handle', 'path/to/myscript.js' ); // Localize the script with new data $translation_array = array( 'some_string' => __( 'Some string to translate', 'plugin-domain' ), 'a_value' => '10' ); wp_localize_script( 'some_handle', 'object_name', $translation_array ); // Enqueued script with localized data. wp_enqueue_script( 'some_handle' ); <script> // alerts 'Some string to translate' alert( object_name.some_string); </script> 添加到-WarningAction Stop命令:

-ErrorAction Stop

答案 1 :(得分:3)

考虑到问题的通用标题,让我首先回顾一下捕获警告如何正常

  • 文件,例如warnings.txt3> warnings.txt

    • 特别禁止警告:3> $null
  • 变量中,例如$warnings-WarningVariable warnings-wv warnings

    • 但是,仍会通过传递警告,因此会打印它们,因此为了防止您 使用3> $null

请注意,这些构造必须应用于生成警告的very命令,因为(默认情况下)只有 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默认生成语句终止错误时生效;要使非终止错误生效,请添加-ErrorAction Stop 如果Remove-MailboxPermission生成只是警告 - 并且完全没有错误 - 则try / catch将不会产生任何影响。
  • warning.txt中没有输出(即使通过删除初始#重新激活该行),因为try / catch仅捕获< em>错误输出,而不是警告;也就是说,在处理catch处理程序时,警告已经打印

答案 2 :(得分: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应该有警告。