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:
- Windows PowerShell Team blog
- Windows PowerShell ScriptCenter
- Windows PowerShell Concepts
- Windows PowerShell Cmdlets
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.
Comments
Anonymous
June 12, 2008
Hey I just discovered this blog and would like to know why MSMQ is tied to the OS. Why isn't MSMQ 4.0 available for XP when WCF is?Anonymous
June 12, 2008
Is it possible to connect to remote private queues via the COM API? Thanks for the post.Anonymous
June 12, 2008
The comment has been removedAnonymous
June 12, 2008
Hi Kris, What are you planning to do with the remote private queue? Just send/receive messages or do you want to administer the queue? If the latter then you are limited on what you can do - create, delete, and get/set properties are only available on the local machine. See item 5.12 in the "MSMQ FAQ" in the "MSMQ Documenttion" list on the left of this blog.Anonymous
July 30, 2008
Hey John We've added some basic MSMQ cmdlets to Powershell Community Extentions (PSCX) http://www.codeplex.com/PowerShellCX They are part of release v.next - not sure when it'll make it out the door, but you can get the code from codeplex and build it locally. I'd be interested in any feedback you might have. ThanksAnonymous
October 08, 2008
Hi, I am interested in reading properties from a remote public queue. Is it possible? I've seen the FAQ briefly for public queues but cannot find answer. What I want to do is read from a public queue on a server with some filters. I would appreciate if you could give me some starting code (if at all possible :) ) ThanksAnonymous
October 08, 2008
Hi, I assume you mean you want to read messages from a public queue based on certain properties of those messages, yes? (The public aspect of the queue is not important here). MSMQ doesn't have any features to enable this - you would normally have to check (Peek) each message in turn to see if it matches your criteria. Alternatively, you may want to look into using LINQ (http://blogs.msdn.com/johnbreakwell/archive/2008/02/24/linq-and-msmq.aspx) to make this easier. Cheers John Breakwell