# Script to get mailbox sizes from both Exchange 2007+ servers and legacy Exchange # 2000 and 2003 Servers # # This script is provided "AS IS" with no warranties, and confers no rights. # Use of included script samples are subject to the terms specified at # https://www.microsoft.com/info/cpyright.htm. # Written by: Gary Siepser, Microsoft, Premier Field Engineer
#Get list of only E2k7 or later mailboxes as we get their size through a cmdlet #, but the mailboxes still on E2K3 have to come from WMI $2007Users = Get-Mailbox -RecipientTypeDetails usermailbox
#Get a list of the legacy servers so we can connect to WMI on each one $legacyservers = get-exchangeserver | Where-Object {-not $_.IsExchange2007OrLater}
#get the stats on all the E2K7 Mailboxes. This may yield a number of red and yellow #errors and warnings respectively. These can be squashed through error and warning #preference. $2007Userstats = $2007Users | get-mailboxstatistics
#loop through the legacy servers to get the userstats through WMI foreach ($server in $legacyservers) { # Use the right WMI namespace and class to get to mailbox data and add it to a building # array to hold it all so we can combine this with the non-legacy user data already # collected $legacyusers += Get-WmiObject -ComputerName $server -Namespace root\MicrosoftExchangeV2 -Class Exchange_mailbox }
#Now we have all the data in two seperate variables. Unfortunately the objects types # in those variables is quite different. We are going to go ahead and create a simple # new object type and populate it with the data from each list to make a new common # list of mailbox names and sizes. #Create the object and give it the properties we want to use $templateobject = New-Object PSObject $templateobject = $templateobject | Select-Object Name,Server,MailboxSizeinMB
# Now we need to loop through the users lists and populate these custom objects # into a new array
#Create the empty array $results = @()
# Loop through the 2007 user stats first foreach ($user in $2007Userstats) { #copy the template object to this actual instance we want to add to the building list $newobjectInstance = $templateobject | Select-Object *
#Populate the properties with the data $newobjectInstance.Name = $user.DisplayName $newobjectInstance.Server = $user.ServerName
#We need to pull out the size in Bytes from the object that is in totalitemsize # and then devide it by 1MB and round it off. This gives us a nice number with # two decimals places...nicer that what the .ToMB() method gives us) $newobjectInstance.MailboxSizeinMB = [Math]::Round(($user.totalitemsize.value.ToBytes() / 1MB),2)
#add this object isntance to the result array $results += $newobjectInstance }
#now loop through the WMI data we got back for the legacy servers and users foreach ($user in $legacyusers) { #copy the template object to this actual instance we want to add to the building list $newobjectInstance = $templateobject | Select-Object *
#Populate the properties with the data $newobjectInstance.Name = $user.MailboxDisplayName $newobjectInstance.Server = $user.ServerName
#We need to pull out the size in Bytes from the object that is in totalitemsize # and then devide it by 1MB and round it off. This gives us a nice number with # two decimals places...nicer that what the .ToMB() method gives us) $newobjectInstance.MailboxSizeinMB = [Math]::Round(($user.size / 1MB *1KB),2)
#add this object isntance to the result array $results += $newobjectInstance }
# Sort the results in a more meaningful way $results = $results | Sort-Object mailboxsizeinMB –Descending
#Do whatever you want now, in this case simply output the results. $results
|