Enumerate queues (Perl)

This code can be found in Chapter 7 of Exchange Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This script was originally published in the Exchange Cookbook,
# (http://www.exchangebookcook.com). Written by Paul Robichaux, 
# Missy Koslosky, and Devin Ganger. Redistributed with permission 
# of the publisher, O'Reilly & Associates.

# This code enumerates the SMTP links and queues on an
# Exchange Virtual Server and only works on Exchange Server 2003.

# ------ SCRIPT CONFIGURATION -----
# host name of the Exchange server

use Win32::OLE qw(in);
use constant vbCrLf => "\r\n";

$strHostname = '<ExchangeServerName>';
# e.g., red-exch02 ' The number of the SMTP virtual server instance
$strVSInstance = '<VSInstanceNumber>';
# e.g., 1 for the default SMTP virtual server
# ------ END CONFIGURATION --------

# Get the Exchange Namespace WMI object
$objWMIExch = Win32::OLE->GetObject('winmgmts://./root/MicrosoftExchangeV2');
# Get the list of Exchange_SMTPLink instances from the default virtual server
# and iterate through them.
$objLinksList = $objWMIExch->ExecQuery('Select * from Exchange_SMTPLink' . ' Where VirtualMachine=\'' . $strHostname . '\' And VirtualServerName=\'' . $strVSInstance . '\'');
foreach my $objLinkInst (in $objLinksList) {
    $strLinkInfo = $strLinkInfo . 'Link Name: ' . $objLinkInst->LinkName . vbCrLf . '  Link ID: ' . $objLinkInst->LinkID . vbCrLf;
    # Now, get the associated Exchange_SMTPQueue instances and iterate through
    # them.
    $objQueuesList = $objWMIExch->ExecQuery('Select * from Exchange_SMTPQueue' . ' Where VirtualMachine=\'' . $strHostname . '\' And VirtualServerName=\'' . $strVSInstance . '\' And LinkID=\'' . $objLinkInst->LinkID . '\' And LinkName=\'' . $objLinkInst->LinkName . '\'');
    foreach my $objQueueInst (in $objQueuesList) {
        $strLinkInfo = $strLinkInfo . '  Queue Name: ' . $objQueueInst->QueueName . vbCrLf . '    Queue ID: ' . $objQueueInst->QueueID . vbCrLf;
    }
    $strLinkInfo = $strLinkInfo . vbCrLf;
}

print "$strLinkInfo\n";

This code has been viewed 628 times.

New from the creators of TechTasks.com: StatSheet.com