Powershell Script: Display Exchange 2007 Queue by Site

18 03 2009

Working in a multi site Exchange 2007 environment can be cumbersome when trying to view queue information on the Hub servers.  I wrote this script to simplify this process.
The script queries the Exchange 2007 organization, finds the servers with the Hub Transport Roles, and grouping them into sites (you will have to define the sites yourself, based on your organizational configuration).  You can find the sites for your organization by entering this command:

Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | sort name

This will output a table of all the Hub Transport servers and list the Site for each.  Take these site names, and change the script with your values.  You can also modify the variable names to match each site’s name.

Script:

# Exchange 2007 Powershell Script
# Script to display queue information of Hub Transport Servers
# Dynamicly selects Exchange Hub Transport Servers from Exchange 2007 Organization
# Selects server's location by Site
# Output queue information
# Writen by Dan Burgess
# nerd@EverydayNerd.com

cls

# Set variables
$last = "begin"
$choice = 'start'

# Dynamicly set variables for each site's hub transport servers, and sort by server name
$usa = Get-ExchangeServer | where { $_.isHubTransportServer -eq $true -and $_.Site -like '*USA*' } | sort name
$eur = Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true -and $_.Site -like '*EUR*' } | sort name
$chi = Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true -and $_.Site -like '*CHI*' } | sort name
$aus = Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true -and $_.Site -like '*AUS*' } | sort name
$afr = Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true -and $_.Site -like '*AFR*' } | sort name
$allhub = Get-ExchangeServer | Where { $_.isHubTransportServer -eq $true } | sort name

# Start WHILE loop, that allows the user to repeat or select the choice selection instead of exiting the script after completion
while ($choice -ne 'x')
{
write-host ""
write-host "0 - All Sites" -foregroundcolor Yellow
write-host "1 - America (USA)" -foregroundcolor Yellow
write-host "2 - Europe (EUR)" -foregroundcolor Yellow
write-host "3 - China (CHI)" -foregroundcolor Yellow
write-host "4 - Austrialia (AUS)" -foregroundcolor Yellow
write-host "5 - Africa (AFR)" -foregroundcolor Yellow
write-host "6 - All Queue with messages higher than 10" -foregroundcolor Green
write-host "X - Exit" -foregroundcolor Red
write-host ""


# If statement to see check if first selection, or sequencial
if($last -eq "begin")
{
[console]::ForegroundColor = "Green"
$choice = read-host "Select site to check Hub Queue's"
[console]::ResetColor()
}
else
{
[console]::ForegroundColor = "Green"
$choice = read-host "Select site to check Hub Queue's (Press Enter to repeat your last selection)"
[console]::ResetColor()
}

switch ($choice)
{
0 { $hub = $allhub }
1 { $hub = $usa }
2 { $hub = $eur }
3 { $hub = $chi }
4 { $hub = $aus }
5 { $hub = $afr }
6 { $hub = $allhub }
x { }
default { $choice = $last }
}

# For each server in the site selected, get the queue information, and list as a table
if($choice -eq "6")
{
"Begin Hub List" > HubQueueOutput.txt
"--------------------------------------" >> HubQueueOutput.txt
foreach ( $server in $hub )
{
get-exchangeserver $server -erroraction silentlycontinue | get-queue -erroraction silentlycontinue | where {$_.MessageCount -gt 10 } >> HubQueueOutput.txt
}
"--------------------------------------" >> HubQueueOutput.txt
"End Hub List" >> HubQueueOutput.txt
more HubQueueOutput.txt
}
elseif($choice -ne "x")
{
"Begin Hub List" > HubQueueOutput.txt
"--------------------------------------" >> HubQueueOutput.txt
foreach ( $server in $hub )
{
get-exchangeserver $server -erroraction silentlycontinue | get-queue -erroraction silentlycontinue >> HubQueueOutput.txt
}
"--------------------------------------" >> HubQueueOutput.txt
"End Hub List" >> HubQueueOutput.txt
more HubQueueOutput.txt
}
else
{
write-host ""
write-host "Script exiting" -foregroundcolor DarkCyan
write-host ""
write-host "Have a nice day..."
write-host ""
write-host ""
}
$last = $choice
# Go back to begining of WHILE loop, and do it again!
}

Of course this has to be ran on a machine that hat the Exchange 2007 Powershell installed.  Hope this helps, and saves time!





Script to Find Whitespace on all Exchange 2007 Servers

23 01 2009

As an Exchange Admin, I’m sure you have had the need to find out how much whitespace is in each of your Exchange databases.  This can be a real pain to do, crawling through the Event Log, looking for Event ID 1221.  This script will query all Exchange 2007 Mailbox servers, crawl through the Event logs, and return the Whitespace for every Database you have.  This information can also be exported to a SQL database, if you un-comment the appropriate lines.

Hope this makes your job easier!

*** Special thanks to my co-worker Stephen M for writing this script, and suggesting that I post it to ExchangeStyle.EverydayNerd.com – Thanks!

# Script: Get-WhiteSpace.ps1 will return the Whitespace on every Mailbox Server in an Exchange 2007 Environment
# Author: EverydayNerd.com
# Contact: Nerd [at] everydaynerd.com

# Uncomment to write to a database

#$db = “Enter Server Name Here”
#$catalog = “Enter Database Catalog Here”
#$conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=$db; Initial Catalog=$catalog; Integrated Security=SSPI")
#$conn.Open()

$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime([DateTime]::UtcNow.AddDays(-1))
$servers = Get-ExchangeServer | Where{$_.ServerRole -eq 'None' -or $_.ServerRole -like '*Mailbox*'} | sort name
foreach ($server in $servers){
Write-Host $server
Get-WmiObject -computer $server -query ("Select * from Win32_NTLogEvent Where Logfile='Application' and Eventcode = '1221' and TimeWritten >='" + $WmidtQueryDT + "'") |
ForEach-Object{
$time = [System.Management.ManagementDateTimeConverter]::ToDateTime($_.TimeGenerated).ToString()
$date = $time.split("' '")
$writedate = $date[0]
$mbnamearray = $_.message.split("' '")
$store = $mbnamearray[2]
$store = $store -replace '"', ''
$size = [math]::round(($mbnamearray[4]/1024),4)
$search = $server.ToString() + "\" + $store.ToString()

$test = Get-ExchangeServer $server
If ($server.ServerRole -eq 'None'){
$root =[ADSI]'LDAP://RootDSE'
$cfConfigRootpath = "
LDAP://" + $root.ConfigurationNamingContext.tostring()
$configRoot = [ADSI]$cfConfigRootpath
$searcher = New-Object DirectoryServices.DirectorySearcher($configRoot)
$searcher.Filter = '(&(objectCategory=msExchExchangeServer)(cn=' + $Server + '))'
$searchres = $searcher.FindOne()
$snServerEntry = New-Object System.DirectoryServices.directoryentry
$snServerEntry = $searchres.GetDirectoryEntry()


$adsiServer = [ADSI]('LDAP://' + $snServerEntry.DistinguishedName)
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($adsiServer)
$dfsearcher.Filter = "
(objectCategory=msExchPrivateMDB)"
$srSearchResults = $dfsearcher.FindAll()
foreach ($srSearchResult in $srSearchResults){
$msMailStore = $srSearchResult.GetDirectoryEntry()
$srnamearray = $_.message.split("
'\'")
$srstore = $mbnamearray[2]
$srstore = $srstore -replace '
"',''
If ($srstore.ToString() -like $store.ToString()){
$EDBFile = $msMailStore.msExchEDBFile
$EDBFilePath = "
\\" + $server.ToString() + "\" + $EDBFile.ToString()
$EDBFilePath = $EDBFilePath -replace ':', '$'
$EDBFileSize = Get-Item $EDBFilePath.ToString() | select Length
}
}
}
Else{
$EDBFile = Get-MailboxDatabase -Identity $search | select EDBFilePath
$EDBFile = $EDBFile -replace '@{EdbFilePath=', ''
$EDBFile = $EDBFile -replace '}', ''
$EDBFile = $EDBFile -replace ':', '$'
$EDBFilePath = "
\\" + $server.ToString() + "\" + $EDBFile.ToString()
$EDBFileSize = Get-Item $EDBFilePath.ToString() | select Length
}

$EDBFileSize = $EDBFileSize -replace '@{Length=', ''
$EDBFileSize = $EDBFileSize -replace '}', ''
$EDBFileSize = [math]::round(((($EDBFileSize/1024)/1024)/1024),2)

# Uncomment to write to a database
#$cmd = $conn.CreateCommand()
#$cmd.CommandText ="
INSERT EventLog1221 VALUES ('$writedate', '$server', '$store', '$size', '$EDBFileSize')"
#$cmd.ExecuteNonQuery()

Write-Host $writedate $server $store $size $EDBFileSize
}
}
# Uncomment to write to a database
#$conn.Close()





Powershell Script to list all Cluster Active Nodes

9 01 2009

If you have a medium to large infrastructure, it is helpful to know what node of your Exchange Clusters is the active node.  This can be done manually, but it is much easier if done with the following script:

 

$Clusters = Get-MailboxServer | Where-Object { $_.RedundantMachines } | Select Name
 
 foreach ($cluster in $clusters) {
    Get-ClusteredMailboxServerStatus -identity $Cluster.name | Select OperationalMachines,State 
 
    }








Design a site like this with WordPress.com
Get started