Inspect queue (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 messages within a given queue.

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

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

$strHostname = '<ExchangeServerName>';
# e.g., red-exch02 ' Name of the SMTP queue to search for messages
$strQueueName = '<SMTPQueueName>';
# e.g., 3sharp.com
# ------ END CONFIGURATION --------

# Get the Exchange Namespace WMI object
$objWMIExch = Win32::OLE->GetObject('winmgmts://' . $strHostname . '/root/MicrosoftExchangeV2');
# Get the list of queues and process our desired queue
$objQueuesList = $objWMIExch->InstancesOf('Exchange_SMTPQueue');
foreach my $objQueueInst (in $objQueuesList) {
    # Make sure this queue is the one we're looking for; if not, skip it
    if ($objQueueInst->QueueName eq $strQueueName) {
        $strMsgInfo = $strMsgInfo . 'Queue: ' . $objQueueInst->QueueName . ' (' . $objQueueInst->QueueID . ')' . vbCrLf;
        $objMsgsList = $objWMIExch->ExecQuery('Select * From ' . 'Exchange_QueuedSMTPMessage Where ProtocolName=\'SMTP\' And LinkId=\'' . $objQueueInst->LinkID . '\' And LinkName=\'' . $objQueueInst->LinkName . '\' And QueueId=\'' . $objQueueInst->QueueID . '\' And QueueName=\'' . $objQueueInst->QueueName . '\' And VirtualMachine=\'' . $objQueueInst->VirtualMachine . '\' And VirtualServerName=\'' . $objQueueInst->VirtualServerName . '\'');
        foreach my $objMsgInst (in $objMsgsList) {
            $strMsgInfo = $strMsgInfo . '  Message ' . $objMsgInst->MessageID . vbCrLf . '    Sender: ' . $objMsgInst->Sender . vbCrLf . '    MessageID: ' . $objMsgInst->MessageId . vbCrLf;
        }
    }
}

print "$strMsgInfo\n";

This code has been viewed 779 times.

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