brain dump far away from /dev/null

20Jan/123

Get a list of all disconnected mailboxes in Exchange 2010

The following script cleans up all your Exchange databases (necessary to get a fresh list of all disconnected mailboxes) and creates an HTML and CSV file containing:

  • DisplayName of the mailbox
  • ServerName, on which the mailbox is stored
  • DatabaseName, in which the mailbox is stored
  • DisconnectDate, when the mailbox was disconnected from the user account (the HTML output is also sorted by this column)
  • DisconnectReason, guess it: why the mailbox was disconnected (i.e. because the user account was disabled)

Just copy&paste these lines into a powershell script:

# ExportDisconnectedMailboxes.ps1
#
# This script runs "Clean-MailboxDatabase" on all
# available databases and creates an HTML report
# of all disconnected mailboxes.
#
# Revision History
# 2012-01-20 Christoph Holas    Initial Version
 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
 
Get-MailboxDatabase | Clean-MailboxDatabase
 
$MailboxDatabases = Get-MailboxDatabase
 
$DiscMailboxes = $MailboxDatabases | ForEach-Object { Get-MailboxStatistics -Database $_.Name | Where-Object {$_.DisconnectDate -ne $null}} | select DisplayName,ServerName,DatabaseName,DisconnectDate,DisconnectReason | sort DisconnectDate
 
$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
 
$DiscMailboxes | ConvertTo-Csv | Out-File DisconnectedMailboxes.csv
$DiscMailboxes | ConvertTo-Html -head $a | Out-File DisconnectedMailboxes.html

Don't hesitate to write a comment if this script was (not) helpful to you. 🙂

Comments (3) Trackbacks (0)
  1. MailboxDatabase | MailboxStatistics | ? {$_.DisconnectDate} | select DisplayName, DisconnectReason, MailboxGuid, DatabaseName, ServerName, DisconnectDate

  2. The below script will let you search disconnected mailboxes. It prompts you to input a portion of the users display name.

    ## sean.m.simon 01/03/2013

    $in = Read-host “Enter a portion of the display name of the disconnected mailbox you are searching for. (example: Lastname, Firstname)”
    Write-Host
    Write-Host -ForeGroundColor Green “********* Searching **********”

    Get-MailboxDatabase |
    Get-MailboxStatistics |
    where {($_.DisconnectReason -ne $null) -And ($_.displayname -like “*$in*”)} |
    select displayname,LastLoggedOnUserAccount,Identity,database,DisconnectDate,disconnectreason,LastLogonTime,TotalItemSize | fl

    Write-Host -ForeGroundColor Green “********* Search Complete **********”
    Write-Host
    Write-Host ‘The following cmdlet is an example of how to reconnect a mailbox to an existing AD account:’
    Write-Host -ForeGroundColor darkcyan ‘Connect-Mailbox -Identity “a3626701-dc92-432a-9cd0-3df34b6f49af” -Database db01 -User firstname.m.lastname’
    Write-Host

  3. Thanks for this, it got me going in the right direction.

    I made a few changes: I added a check to see if the snapin is loaded and only load it if it is not, I added the tag BODY to the head section because in my case it would not display correctly in HTML without it and I added a date stamp to the file name to keep a history of them. I had added this to a task in the server and it runs every morning.

    if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue) -eq $null )
    {
    Add-PsSnapin Microsoft.Exchange.Management.PowerShell.E2010
    }

    Get-MailboxDatabase | Clean-MailboxDatabase

    $MailboxDatabases = Get-MailboxDatabase

    $DiscMailboxes = $MailboxDatabases | ForEach-Object { Get-MailboxStatistics -Database $_.Name | Where-Object {$_.DisconnectDate -ne $null}} | select DisplayName,Identity,ServerName,DatabaseName,DisconnectDate,DisconnectReason,LastLoggedOnUserAccount,LastLogonTime,TotalItemSize | sort DisconnectDate

    $a = “”
    $a = $a + “BODY{background-color:white;}”
    $a = $a + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
    $a = $a + “TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}”
    $a = $a + “TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}”
    $a = $a + “”

    $Today = [string]::Format( “{0:MM-dd-yyyy}”, [datetime]::Now.Date )
    $FileName = “DisconnectedMailboxes_$Today”

    $DiscMailboxes | ConvertTo-Csv | Out-File $FileName.csv
    $DiscMailboxes | ConvertTo-Html -head $a | Out-File $FileName.html


Leave a comment

No trackbacks yet.