Dipping my toes into monitoring MSMQ with code (#1 in a series)

I don't write much code - I leave that to people who can - but I'm coming round to the idea that maybe I should invest time in making some custom scripts to help me (and customers) troubleshoot MSMQ.

So here are my first efforts with some basic VBScript samples ( kindly started for me by Ruud Baars ) that make use of MSMQ's COM API to look at outgoing queues.

The APIs of interest are:

  • MSMQApplication
    use this to obtain a list of the queues, and then...
  • MSMQManagement
    use this to collect generic data, like how many messages, and then...
  • MSMQOutgoingQueueManagement
    use to collect data specific to outgoing queues, such as the number of times the last message was resent

I'm only using the first two in this demonstration as I soon found that trying to call the implicitly created MSMQOutgoingQueueManagement object won't work in VBScript. So next time I'll use Powershell or maybe C#.

How to check the number of messages in an outgoing queue

Dim PlumbApp
Dim PlumbManage
Dim PlumbQFormatName

set PlumbApp = CreateObject("MSMQ.MSMQApplication")
PlumbQArray = PlumbApp.ActiveQueues
For i = LBound(PlumbQArray) To UBound(PlumbQArray)
     set PlumbManage = CreateObject("MSMQ.MSMQManagement")
     PlumbQFormatName = PlumbQArray(i)
     PlumbManage.Init ,,PlumbQFormatName
     if  PlumbManage.IsLocal = 0 Then
        if PlumbManage.MessageCount <> 0 Then
            Wscript.Echo "In Outgoing Queue Format Name = ", PlumbQFormatName
            Wscript.Echo "There are " & PlumbManage.MessageCount & " messages"
            Wscript.Echo "These messages occupy " &  cdbl(PlumbManage.BytesInQueue)  & " bytes of storage space"
        end if
     end if
Next

which outputs something like:

In Outgoing Queue Format Name =  DIRECT=os:PlumbersMateprivate$testtx
There are 3 messages
These messages occupy 60840 bytes of storage space
In Outgoing Queue Format Name =  DIRECT=os:PlumbersMateprivate$test
There are 1 messages
These messages occupy 20256 bytes of storage space

Note - I had to convert PlumbManage.BytesInQueue to double as it returns a Variant (unsigned 64-bit integer) that VBScript can't handle directly. Another reason to move on from VBScript, maybe...