我有一個腳本,它將Active Directory組的成員添加到Office 365通訊組中。 AD組的許多成員也將是通訊組已,這將導致腳本顯示錯誤的成員:抑制Office365中的異常powershell
Adding [email protected] to distribution group GROUP
The recipient "[email protected]" is already a member of the group "GROUP".
+ CategoryInfo : NotSpecified: ([email protected]:RecipientWithAdUserGroupIdParameter`1)
[Add-DistributionGroupMember], MemberAlreadyExistsException
+ FullyQualifiedErrorId : [Server=HKNPR04MB0531,RequestId=84dc77fb-8cf4-4e2f-882e-0ce66b735d08,TimeStamp=9/02/2015 6:55:13 AM] [FailureCategory=Cmdlet-MemberAlreadyExistsException] 7CEFF683,Microsoft.Exchange.Management.RecipientTasks.AddDistributionGroupMember
+ PSComputerName : pod51055psh.outlook.com
我想取消這些錯誤,因爲我不,如果已經是會員關懷存在。
我試圖抓住MemberAlreadyExistsException
,設置-ErrorAction SilentlyContinue
並捕獲所有錯誤並編寫「錯誤!」而不是實際的例外,但是這似乎沒有生效。
目前,我的try-catch塊看起來是這樣的:
try
{
Add-DistributionGroupMember -Identity $DistributionGroupName -Member $MemberEmail
}
Catch [System.Management.Automation.RemoteException]
{
if($_.FullyQualifiedErrorId -match 'AlreadyExists')
{
Write-Output "`t $emailaddress is already a member of $DistributionGroupName."
}
else
{
Write-Output "`t $_.Exception"
}
}
我相信,當一個用戶已經存在這應該提醒我,但我仍然收到異常信息。
這可能不是一個終止錯誤。如果將-ErrorAction Stop添加到Add-DitributionGroupMember cmdlet,它會起作用嗎? – mjolinor 2015-02-09 13:13:14