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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…