Eliminar buzones de usuarios deshabilitados en Exchange 2010

Aquí os pongo un script que borra los buzones de Exchange de los usuarios deshabilitados en AD. Comprueba que sean del tipo UserMailbox, porque hay buzones como los RoomMailbox cuyo usuario está deshabilitado, cuidado con esto.

Es necesario tener instalado los comandos powershell para Active Directory de Quest.

# Script que deshabilita los buzones de todos los usuarios de AD deshabilitados

Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue | Out-Null
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue | Out-Null

$DisabledMailboxes = New-Object -TypeName Microsoft.Exchange.Data.Directory.Management.Mailbox
$DisabledMailboxes = $null
Get-QADUser -Disabled -SizeLimit 0 | foreach-object {
   $ID = $_.samaccountname
   $mbx = Get-Mailbox -Identity $ID -RecipientTypeDetails UserMailbox  -ErrorAction SilentlyContinue
   If ($mbx) {
      Write-Host "Añado a la lista: " $mbx.Name
      $DisabledMailboxes += $mbx
   }
}
If ($DisabledMailboxes) {
   $DisabledMailboxes | foreach-object {
      Write-Host "Deshabilito el buzón: " $_.Name
      $_ | Disable-Mailbox -Confirm:$false
   }
}
Get-MailboxDatabase | Clean-MailboxDatabase
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s