删除内部共享邮箱

问题描述

下面是我试图执行以删除共享邮箱的脚本。我们有本地和 oncloud 共享邮箱ex,以下脚本适用于 oncloud 共享邮箱,但不适用于本地,即使它显示成功执行。

我应该使用 Remove-ADUser 而不是 Remove-RemoteMailBox

Try{
$SharedMailBoxUPN = $payload.sharedMailBoxUPN
$isOnPrem = ""
#Connect to Exchange-Online
Try
{ #FIND OUT If Shared MailBox is On-Prem or OnCloud
$isOnPrem = Get-MailBox -Identity $SharedMailBoxUPN | Select-Object -ExpandProperty RemoteRecipientType
}
Catch
{
    #ERROR MESSAGE GOES HERE
}
if($isOnPrem -eq "Migrated,SharedMailBox") #OnPrem MailBox
{
$sessionCheck = Get-PSSession
##### Connect to Exchange On-Prem Powershell #####
    if ($sessionCheck -eq $null)
    {
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$TargetServer/PowerShell/ -Authentication Kerberos -Credential $cred
        Import-PSSession $Session
    }
}
Try 
{
Remove-RemoteMailBox -Identity $SharedMailBoxUPN -Confirm:$false -ErrorAction Stop #Shows result as Success,but mailBox is not removed for some reason

Remove-ADUser -Identity $SharedMailBoxUPN #SHOULD I USE this instead??

}
Catch 
{
     #ERROR MESSAGE GOES HERE
}
}
else #oncloud mailBox
{
Try
{
     #Connect to ExchangeOnline
    Remove-MailBox -Identity $SharedMailBoxUPN -Confirm:$false -ErrorAction Stop
   
}
Catch
{
     #ERROR MESSAGE GOES HERE
}
}    
}
}
Catch
{
 #ERROR MESSAGE GOES HERE
}

解决方法

Remove-RemoteMailbox 仅适用于您的云共享邮箱,您可能希望 Remove-Mailbox 删除本地共享邮箱。使用 Remove-ADUser 将删除对象,但您应该真正使用 Exchange cmdlet 来执行此操作,否则最终可能会出现意外结果,例如孤立邮箱等