移除内部共享邮箱



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

我应该使用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 cmdlets来做到这一点,否则你可能会得到意想不到的结果,如孤立的邮箱等

最新更新