Exchange 2003 and Windows Powershell

I just have to share this with you, as I was quite impressed by how quick I knocked this script up.  Basically I wanted to see how much email was queued up on my Exchange 2003 servers.  I had a feeling that a WMI object existed so I ran scriptomatic on a server a behold under rootMicrosoftExchangeV2 was Exchange_SMTPQueue.

So the rest was easy.  Using the built in Powershell command Get-WmiObject I was able to query an Exchange 2003 server for it’s SMTP queues and message count:

Get-WmiObject -namespace “rootMicrosoftExchangeV2” Exchange_SMTPQueue -computername

So now all I needed to do was give it a list of servers to run this against.  I came up with this.  Now c:ps_PublicFolderServerList.txt is a CSV file with two columns, ServerName and Version.  Version can be Exchange2000, Exchange2003 or Exchange2007

$Servers = Import-Csv “c:ps_PublicFolderServerList.txt”

$Server = $Servers | Sort-Object $_.ServerName
$AllQ = @()
$SrvList = @()
ForEach ($Server in $Servers)
{
    If ($Server.version -eq “Exchange2003”)
    {
        $Srv = $Server.ServerName
        Write-host “Checking .. $Srv”
        $AllQ += Get-WmiObject -namespace “rootMicrosoftExchangeV2” Exchange_SMTPQueue -computername $Srv | where {$_.MessageCount -gt 0} | select VirtualMachine, QueueName, MessageCount
    }
}
$AllQ = $AllQ | Sort-Object $_.MessageCount

ForEach ($q in $AllQ)
{

    Write-Host $q.VirtualMachine -NonewLine
    Write-host “: Q Name=” -NonewLine
    Write-Host $q.QueueName -NoNewLine
    Write-host “: ” -NonewLine
    Write-host $q.MessageCount
}

Now if you have a mixed 2003/2007 environment, you can use the exchange snapin to dynamically get the Exchange 2003 servers.  That looks like this, you can see the difference at the top.  Now all I need is this in a GUI (I done this too and will blog later)

$Server = $Servers | Sort-Object $_.ServerName
$AllQ = @()
$SrvList = @()

$Servers = Get-ExchangeServer | where {$_.IsExchange2007OrLater -eq $False}
$Servers = $Servers | Sort-Object $_.Name
ForEach ($Server in $Servers)
{
    $tmpMSXVer = $Server.AdminDisplayVersion.ToString()   
    IF ($tmpMSXVer.IndexOf(“6.5 (“) -ge 0)
    {
        $Srv = $Server.Name

        Write-host “Checking .. $Srv”
        $AllQ += Get-WmiObject -namespace “rootMicrosoftExchangeV2” Exchange_SMTPQueue -computername $Srv | where {$_.MessageCount -gt 0} | select VirtualMachine, QueueName, MessageCount
    }
}

$AllQ = $AllQ | Sort-Object $_.MessageCount

ForEach ($q in $AllQ)
{

    Write-Host $q.VirtualMachine -NonewLine
    Write-host “: Q Name=” -NonewLine
    Write-Host $q.QueueName -NoNewLine
    Write-host “: ” -NonewLine
    Write-host $q.MessageCount
}

Post using Windows Live Writer via my HTC Shift

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.