删除内部共享邮箱

时间:2021-01-25 12:49:39

标签: powershell active-directory exchange-server

下面是我试图执行以删除共享邮箱的脚本。我们有本地和 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
}

1 个答案:

答案 0 :(得分:0)

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