Delete message from queue (VBScript)

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 VBScript 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 deletes a given message within an Exchange SMTP queue.

' ------ SCRIPT CONFIGURATION -----
' The host name of the Exchange server
strHostname = "<ExchangeServerName>" ' e.g., red-exch02
' Name of the SMTP queue to search for messages
strQueueName = "<SMTPQueueName>" ' e.g., 3sharp.com
' Message ID of the message to delete
strMessageID = "<MessageID>"
' ------ END CONFIGURATION --------

' Get the Exchange Namespace WMI object
Set objWMIExch =  GetObject("winmgmts://" & strHostname &_
   "/root/MicrosoftExchangeV2")
' Get the list of queues and process our desired queue
Set objQueuesList = objWMIExch.InstancesOf("Exchange_SMTPQueue")
For Each objQueueInst in objQueuesList
   ' Make sure this queue is the one we're looking for; if not, skip it
   If objQueueInst.QueueName = strQueueName Then
      strMsgInfo = strMsgInfo & "Queue: " & objQueueInst.QueueName & " (" &_
         objQueueInst.QueueID & ")" & VbCrLF
      Set 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 & "' And MessageID ='" &_
         strMessageID & "'")
      For each objMsgInst in objMsgsList
         strMsgInfo = strMsgInfo & "  Message " &_
            objMsgInst.MessageID & VbCrLF &_
            "    Sender: " & objMsgInst.Sender & VbCrLF &_
            "    MessageID: " & objMsgInst.MessageId & VbCrLF
         objMsgInst.DeleteNoNDR
         strMsgInfo = strMsgInfo & "  Message deleted." & VBCrLF
      Next
   End If
Next

Wscript.Echo strMsgInfo 

This code has been viewed 1918 times.

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