Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

powershell - Removing OnPremise Shared Mailbox

Below is the script I am trying to execute to remove Shared Mailboxes. We have on-prem as well as oncloud shared mailboxex, the below script works fine for oncloud shared mailboxes, but it does not work for on-prem, even though it shows successful execution.

Should I be using Remove-ADUser instead of 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
}
question from:https://stackoverflow.com/questions/65885113/removing-onpremise-shared-mailbox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove-RemoteMailbox will only work for your cloud shared mailboxes, you would want Remove-Mailbox to remove on prem shared mailboxes. Using Remove-ADUser will remove the object but you should really use the Exchange cmdlets to do this or you could end up with unexpected results like orphaned mailboxes etc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...