捕获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
2>c:\temp\errors.txt
->$\c:\temp\errors.txt
-$
表示当前的异常。(2>c:\temp\errors.txt
所做的是通过成功输出流将integer2
(字符串化)发送到文件中)。-在这种情况下,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