Total Number of Mailboxes in Exchange Environment
Hi Folks
I am trying to identify the total number of mailboxes in our exchange environment. Our Exchange database is spread across 12 different servers, located in different Datacentres.
I have a development environment, which is a replica of the production.
Currently i am using this script in a test environment to extract the total number of mailboxes and it works fine, as i have only one exchange server, which is local. I am quite not sure, how do i extract reports from these 12 different servers.
## PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command ". 'C:\Program Files\Microsoft Identity Integration Server\MaData\count1.ps1'"
$MailboxCount = "C:\Program Files\Microsoft Identity Integration Server\MaData\MailboxCount.csv"
$AllServers = @()
foreach ($server in Get-MailboxServer)
{
foreach ($objItem in Get-MailboxDatabase -server $server)
{
$intUsers = ($objitem | Get-Mailbox -ResultSize Unlimited).count
$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity
$ReturnedObj | Add-Member NoteProperty -Name "User Mailbox Count" -Value $intUsers
$AllServers += $ReturnedObj
}
}
$AllServers | Sort-Object | export-csv $MailboxCount -notype -force
OUTPUT OF THE SCRIPT:
"Server\StorageGroup\Database","User Mailbox Count"
"Server XYZ\First Storage Group\Mail Database","18"
"Server XYZ\Storage Group 2\Mail database 3",
"Server XYZ\First Storage Group\Mail Database 2","12"
This script seems to be halting or not working in the production environment, wondering where i went work, Would appreciate your inputs regarding this.
Best Regards
Suhaas
May 28th, 2010 12:09pm
Copy and paste the entire code into notepad
Change the server name, into your server name (ServerList = Array("server1", "server2", "server3")
Save the file on your Hard drive as mailboxCount.vbs ( c:\ mailboxCount.vbs)
Now go to command line
Drill down to same directory ( c:\ mailboxCount.vbs)
Run the file by typing, c:\ mailboxCount.vbs
Option Explicit
On Error Resume Next
Dim ServerList ' List of computers to check
Dim server ' Current computer to check
Dim fso ' File System Object
Dim strWinMgmts ' Connection string for WMI
Dim objWMIExchange ' Exchange Namespace WMI object
Dim listExchange_Mailboxs ' ExchangeLogons collection
Dim objExchange_Mailbox ' A single ExchangeLogon WMI object
Dim logfile ' Output file
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_Mailbox"
Const LOG_FILE = "EMailSize.csv"
'--------------------------------------
' Set up the array of email servers
'--------------------------------------
ServerList = Array("server1", "server2", "server3")
'--------------------------------------
' Set up log file
'--------------------------------------
set fso = CreateObject("Scripting.FileSystemObject")
Set logfile = fso.CreateTextFile(LOG_FILE)
logfile.WriteLine("""Display Name"",""Mailbox Size"",""Mailbox TotalItems"",""Mailbox StoreName"",""Mailbox ServerName""")
' Create the object string, indicating WMI (winmgmts), using the
' current user credentials (impersonationLevel=impersonate),
' on the computer specified in the constant cComputerName, and
' using the CIM namespace for the Exchange provider.
WScript.Echo "Starting now"
'The rest of the script will fetch mailbox sizes for our servers. Mailbox sizes are in Kilobytes.
For Each server in ServerList
WScript.Echo "Starting " & server & " search."
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & server & "/" & cWMINameSpace
'WScript.Echo strWinMgmts
Set objWMIExchange = GetObject(strWinMgmts)
' Verify we were able to correctly set the object.
If Err.Number <> 0 Then
WScript.Echo "ERROR: Unable to connect to the WMI namespace."
Else
'The Resources that currently exist appear as a list of
'Exchange_Mailbox instances in the Exchange namespace.
Set listExchange_Mailboxs = objWMIExchange.InstancesOf(cWMIInstance)
' Were any Exchange_Mailbox Instances returned?
If (listExchange_Mailboxs.count > 0) Then
' If yes, do the following:
' Iterate through the list of Exchange_Mailbox objects.
For Each objExchange_Mailbox in listExchange_Mailboxs
' Display the value of the Size property.
logfile.WriteLine("""" & objExchange_Mailbox.MailboxDisplayName & """,""" & objExchange_Mailbox.Size & """,""" & objExchange_Mailbox.TotalItems & """,""" & objExchange_Mailbox.StoreName
& """,""" & objExchange_Mailbox.ServerName & """")
Next
Else
' If no Exchange_Mailbox instances were returned, display that.
WScript.Echo "WARNING: No Exchange_Mailbox instances were returned."
End If
End If
Next
Wscript.Echo "Completed"
___________________________________________________________________________
Have a look into these link :
http://www.powershellcommunity.org/Forums/tabid/54/aff/3/aft/1110/afv/topic/Default.aspx
http://gallery.technet.microsoft.com/ScriptCenter/en-us/6d83efbd-5e05-4ed0-b854-9b6e3509aaa3
There are whole lot to script here related to you cause :
http://gallery.technet.microsoft.com/ScriptCenter/en-us/site/search?f[0].Type=RootCategory&f[0].Value=messaging&f[0].Text=Messaging%20%26%20Communication&f[1].Type=SubCategory&f[1].Value=exchange2007&f[1].Text=Microsoft%20Exchange%202007&pageIndex=4 Ripu Daman Mina | MCSE 2003 & MCSA Messaging
Free Windows Admin Tool Kit Click here and download it now
May 28th, 2010 12:59pm
The delay is probably due to difference in scale between the two environments.
If you just need a count of the number of mailboxes in each database, you can also do this:
get-mailbox -resultsize unlimited | select database | group database | sort name | select name,count[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
May 28th, 2010 3:51pm
Hi Ripu
The script reports an error stating "WScript.Echo "ERROR: Unable to connect to the WMI namespace", was wondering where i went wrong. Was also curious to know if i could addan email functionality to this script. To be specific, once the EmailSize.csv is generated,
can i email to a monitoring mailbox ID?
Regards
Justin
Free Windows Admin Tool Kit Click here and download it now
June 8th, 2010 6:13am
Hi Justin,
Kindly try this :
Have a look into this article fro more info :
http://unlockpowershell.wordpress.com/
You will find a lot of script in this that might help you cause.
# Script to show the size and mailbox count of all databases
# Karl Mitschke 4/9/2008
# Modified 1/26/2010 to gather whitespace information
#requires -pssnapin Microsoft.Exchange.Management.PowerShell.Admin
#requires -version 2
# Get-ExchangeWhiteSpace from Shay Levi’s blog
# http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/08/13/exchange-white-space.aspx
function Get-ExchangeWhiteSpace {
param(
$ComputerName = $(throw "ComputerName cannot be empty.")
)
# Convert Dates to WMI CIM dates
$tc = [System.Management.ManagementDateTimeconverter]
$Start =$tc::ToDmtfDateTime( (Get-Date).AddDays(-1).Date )
$End =$tc::ToDmtfDateTime( (Get-Date).Date)
#Create a hash Table to hold the freespace information
$whiteSpace = @{}
# Create two claculated properties for InsertionStrings values
$DB = @{Name="DB";Expression={$_.InsertionStrings[1]}}
$FreeMB = @{Name="FreeMB";Expression={[int]$_.InsertionStrings[0]}}
$freespace = Get-WMIObject Win32_NTLogEvent -ComputerName $ComputerName -Filter "LogFile=’Application’ AND EventCode=1221 AND TimeWritten>=’$Start’ AND TimeWritten<=’$End’" | Select-Object $DB,$FreeMB | Sort-Object
FreeMB –Unique –Descending
$freespace | ForEach-Object {$whiteSpace.Add($_.DB,$_.FreeMB)}
}
$date = ( get-date ).ToString(‘MM-dd-yyyy’)
$AllServers = @()
foreach ($server in Get-MailboxServer)
{
. Get-ExchangeWhiteSpace $server
foreach ($objItem in Get-MailboxDatabase -server $server)
{
$edbfilepath = $objItem.edbfilepath
$path = "`\`\" + $server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2)
$dbsize = Get-ChildItem $path
$dbpath = $EdbFilePath.PathName.Remove(0,2).remove($EdbFilePath.PathName.length-6)
$mailboxpath = "$server$dbpath"
$size = get-wmiobject -computername $server win32_logicaldisk |where-object {$_.deviceid -eq $objItem.EdbFilePath.DriveName} |select-object deviceID, Freespace, Size
$freespace = ($size.freespace / 1GB)
$total = ($size.size / 1GB)
$PercentFree = "{0:n2}" -f ($freespace / $total *100)
$mailboxcount = Get-MailboxStatistics -database "$mailboxpath" |Where {$_.DisconnectDate -eq $null -and $_.ObjectClass -eq ‘Mailbox’} |measure-object
$disconnectedmailboxcount = Get-MailboxStatistics -database "$mailboxpath" |Where {$_.DisconnectDate -ne $null} |measure-object
$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Total Size (GB)" -Value ("{0:n2}" -f ($total))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Used Space (GB)" -Value ("{0:n2}" -f ($dbsize.Length/1024MB))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Free Space (GB)" -Value ("{0:n2}" -f ($freespace))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Percent Disk Free" -Value $percentfree
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "User Mailbox Count" -Value $mailboxcount.count
$dbasename = $objitem.Identity.parent.name +"\" + $objitem.Identity.name
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "White Space (GB)" -Value ("{0:n2}" -f ($whiteSpace[$dbasename]/1024))
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Total Free (GB)" -Value ("{0:n2}" -f ($freespace + $whiteSpace[$dbasename]/1024))
$TotalPercent = ($freespace + $whiteSpace[$dbasename]/1024) / $total *100
$ReturnedObj | Add-Member -MemberType NoteProperty -Name "Total Percent Free" -Value ("{0:n2}" -f ($TotalPercent))
$AllServers += $ReturnedObj
}
}
$body = "<font color=blue>Mailbox database size report for $date<br /><br />"
$bodydetail = $allservers |sort-object "Server\StorageGroup\Database" |convertto-html
$body = $body + $bodydetail
Send-MailMessage -To "kmitschke@contoso.com" -Body $body -Subject "Mailbox Database Size Report" -SmtpServer "mail.contoso.com" -BodyAsHtml:$true -From mailboxdatabaseizereport@noreply.com
Ripu Daman Mina | MCSE 2003 & MCSA Messaging
June 8th, 2010 2:07pm
Hey Ripu
A similar version of the script and it helped me do the job. Thanx for the pointers :-)
$exchangeservers = Get-ExchangeServer |where-object {$_.admindisplayversion.major -eq 8 -and $_.IsMailboxServer -eq $true }
$AllServers = @()
foreach ($server in $exchangeservers)
{
$db = Get-MailboxDatabase -server $server
foreach ($objItem in $db)
{
$mailboxcount = Get-MailboxStatistics -database $objItem |measure-object
$ReturnedObj = New-Object PSObject
$ReturnedObj | Add-Member NoteProperty -Name "Server\StorageGroup\Database" -Value $objItem.Identity
$ReturnedObj | Add-Member NoteProperty -Name "Mailbox Count" -Value $mailboxcount.count
$AllServers += $ReturnedObj
}
}
$AllServers |export-csv c:\test4.csv -notype -force
Free Windows Admin Tool Kit Click here and download it now
June 8th, 2010 2:27pm