【发布时间】:2021-04-29 06:41:24
【问题描述】:
以下是我尝试执行以删除共享邮箱的脚本。我们有 on-prem 和 oncloud 共享邮箱ex,下面的脚本适用于 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
}
【问题讨论】:
标签: powershell active-directory exchange-server