Using PowerShell in a Mixed 2003/7 Exchange Environment
Tuesday, May 13, 2008 at 1:00 Most Exchange admins will have learned, to their distress or delight, that Exchange 2007 has PowerShell at the core of its management. In fact, if you're not aware of that, the GUI seems really odd - you've got to adjust your thinking somewhat and remember it's actually closer to the command line than the old Exchange System Manager. Actually, that was really the catalyst for me getting into PowerShell so much.
Anyway, Exchange 2007 and PowerShell work great together and I wish that I was in a position to take full advantage, but I guess like a lot of people, we're in the process of migrating to Exchange 2007 and will have some Exchange 2003 servers for a while yet. That means that we have our hands tied to a certain extent, but it doesn't mean that we can't do a lot with PowerShell!
The PowerShell function that I've written here is a good example of how you can use the new Exchange 2007 cmdlets to a certain extent and the limitations of that backwards compatiblity. Since we get a lot of requests for quota increases, what I've set out to do is find out the quota limits and current usage when given a username. We also have a slightly strange situation with SMTP addresses (more on that later), so I'm listing those out, as well as the mailbox location...
function get-mailboxinfo ([string]$user = $(Throw "USAGE: get-mailboxinfo username"))
{
$usermb = Get-Mailbox $user
"{0,-15}{1,-2}{2,-20}" -f "User",":",$user
"{0,-15}{1,-2}{2,-40}" -f "Name",":",$($usermb.DisplayName)
"{0,-15}{1,-2}{2,-20}" -f "Server",":",$($usermb.ServerName.ToUpper())
"{0,-15}{1,-2}{2,-40}" -f "Database",":",$($usermb.Database)
"-- SMTP Addresses --"
($usermb.EmailAddresses |?{$_.IsPrimaryAddress -and ($_.PrefixString -eq "SMTP")}|select addressstring).AddressString + " (primary)"
$usermb.EmailAddresses |?{!($_.IsPrimaryAddress)}|select addressstring | %{$_.AddressString}
"-- Quota & Usage --"
if ($usermb.UseDatabaseQuotaDefaults -eq $TRUE){
write-Host "Using database default quotas"
$WarningPreference = "SilentlyContinue" #get-mailboxdatabase generates warnings if .edb in a root directory
#Need to use get-mailboxdatabase in this way to work with Exchange 2003...
#If only using Exchange 2007, get-MailboxDatabase $usermb.Database is better...
$mbdatabase = Get-MailboxDatabase -IncludePreExchange2007 -Server $usermb.ServerName | ?{$_.name -eq $usermb.Database}
$iwq = $mbdatabase.IssueWarningQuota.Value.ToKB()
$psq = $mbdatabase.ProhibitSendQuota.Value.ToKB()
$psrq = $mbdatabase.ProhibitSendReceiveQuota.Value.ToKB()}
else{
$iwq = $usermb.IssueWarningQuota.Value.ToKB()
$psq = $usermb.ProhibitSendQuota.Value.ToKB()
$psrq = $usermb.ProhibitSendReceiveQuota.Value.ToKB()}
if ($usermb.RecipientTypeDetails -eq "LegacyMailbox"){
#Use WMI to find size of Exchange 2003 mailbox
$filter = "MailboxGUID='{" + $usermb.ExchangeGuid + "}'"
$wmimb = gwmi -ComputerName $usermb.Servername -Namespace "root\MicrosoftExchangeV2" -Class "Exchange_mailbox" -filter $filter
$mbsize = $wmimb.size}
else{
#WMI MicrosoftExchangeV2 namespace replaced by get-mailboxstatistics cmdlet in Exchange 2007
$mbsize = (Get-MailboxStatistics $user).TotalItemSize.Value.ToKB()}
"{0,-15}{1,-2}{2,7}" -f "Using (KB)",":",$mbsize
"{0,-15}{1,-2}{2,7}{3,8}{4,8}" -f "Quotas",":",$iwq,$psq,$psrq
"{0,-15}{1,-2}{2,7:p}{3,8:p}{4,8:p}" -f "Quota %",":",$($mbsize/$iwq),$($mbsize/$psq),$($mbsize/$psrq)
}
What that will give you, is a quick report like this, irrespective of where the mailbox is:
User : fbloggs
Name : Fred Bloggs
Server : EXSERVER1
Database : Mailbox Store 6
-- SMTP Addresses --
fred.bloggs@yourdomain.com (primary)
fred.bloggs@marketing.yourdomain.com
-- Quota & Usage --
Using (KB) : 385911
Quotas : 500000 600000 1000000
Quota % : 77.18 % 64.32 % 38.59 %
You may not find the SMTP address list so helpful, but our users each have four (and an X400 address) like:
username@theexchangesystem.domain.com (initially set as the primary by the recipient update policy)
username@forestrootdomain.domain.com
emailname@shortdomain.com (external address with abbreviated domain name, so most used)
emailname@longdomainname.com (the primary external formal email address)
so you can see how it's useful in our case!
If you want to take advantage of Exchange 2007 and the extra info that you can easily get about those mailboxes, add this into the function and wish that migration would move a little faster...
if ($usermb.RecipientTypeDetails -eq "UserMailbox")
{get-MailboxStatistics $user | FT ItemCount,StorageLimitStatus,@{label="TotalItemSize (KB)";expression={$_.TotalItemSize.Value.ToKB()} },LastLogonTime -auto
write-Host "Largest folders for $($user):"
get-MailboxFolderStatistics $user | Sort foldersize,folderpath -desc | Select-Object -first 10 | FT FolderPath,ItemsInFolder,@{label="FolderSize (KB)";expression={$_.FolderSize.ToKB()}} -auto
}


