MSMQ and Powershell #1

Powershell is looking to be a really great scripting language and ideal for filling the void of scripts available for manipulating MSMQ.

Over the next few weeks I plan to generate some simple scripts that will perform some of the basic, and not so basic, administrative and troubleshooting tasks you need to do from time to time. There are no cmdlets written for MSMQ just yet so we will be calling into COM and System.Messaging.

Powershell reference links:

Today's sample will be look at the active queues on a machine.
Note - "Active" means any queue containing messages or being held open by an application.

I have to admit I cheated by running "set-ExecutionPolicy unrestricted" to make things easier. Don't try that in production!

First we need an MSMQApplication object:

$MyMSMQApp = new-object  –comObject  MSMQ.MSMQApplication

If we call the ActiveQueues method then a list (an array of Variants) should be generated:

$MyMSMQApp.ActiveQueues

DIRECT=OS:johnbrea64.mydomain.comprivate$testtx
DIRECT=os:amachineprivate$testtx
DIRECT=OS:johnbrea64.mydomain.comprivate$test

$MyArray = $MyMSMQApp.activequeues

$MyArray.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

$MyArray[1]

DIRECT=os:amachineprivate$testtx

which allows us to analyse each active queue, one at a time, using MQMQQueueInfo.

$MyQueueInfo = new-object -comObject MSMQ.MSMQQueueInfo

$MyQueueInfo.FormatName = $myarray[1]

QueueGuid : {00000000-0000-0000-0000-000000000000}
ServiceTypeGuid : {00000000-0000-0000-0000-000000000000}
Label :
PathName :
FormatName : DIRECT=os:amachineprivate$testtx
IsTransactional : 0
PrivLevel : 1
Journal : 0
Quota : -1
BasePriority : 0
CreateTime : 01/01/1970 00:00:00
ModifyTime : 01/01/1970 00:00:00
Authenticate : 0
JournalQuota : -1
IsWorldReadable :
PathNameDNS :
Properties :
Security :
IsTransactional2 : False
IsWorldReadable2 :
MulticastAddress :
ADsPath :

Now I need to work out how to work with MSMQManagement as that's not so obvious.