Welcome to MSDN Blogs Sign in | Join | Help

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.com\private$\testtx
DIRECT=os:amachine\private$\testtx
DIRECT=OS:johnbrea64.mydomain.com\private$\test

$MyArray = $MyMSMQApp.activequeues

$MyArray.GetType()

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

$MyArray[1]

DIRECT=os:amachine\private$\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:amachine\private$\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.

Published Thursday, June 12, 2008 7:54 PM by JohnBrea

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Thursday, June 12, 2008 3:26 PM by someone

# re: MSMQ and Powershell #1

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?

Thursday, June 12, 2008 6:10 PM by Kris

# re: MSMQ and Powershell #1

Is it possible to connect to remote private queues via the COM API?

Thanks for the post.

Friday, June 13, 2008 6:40 AM by JohnBrea

# Why is MSMQ tied to the OS?

That's a very good question. I don't have the answer but I can propose some possible explanations.

1 The cost of developing is reduced by only working on the next version and not back-porting any new functionality. This applies not only to the time spent on writing code but also to the considerable extra investment in testing and post-sales support.

2 Some features require OS-specific elements which aren't present in older versions - for example, Multicast queues require the PGM network stack. It could be argued that the dependencies could also be back-ported but that may not be practical in all situations.

3 From a sales perspective, there needs to be an feature-rich incentive to upgrade to the next operating system.

The cost incurred by developing MSMQ has to be balanced out by its impact on the sales of new operating systems. Although I think MSMQ is great, it is not a flagship product like SQL Server and so its slice of the development pie is not big enough to make versions that are not tied to the operating system.

It may be easier (for me, anyway) to compare MSMQ with Internet Explorer rather than WCF for this discussion. Internet Explorer is written to work on multiple OSes even though tying it to the OS would be easier. The cost of developing IE is high because it must work cross-platform - hotfixes, for example, are a comparative nightmare to write, test and deploy. But IE is a flagship product and so the business model is completely different. The success of IE is important to Microsoft's brand as a whole and not a simple discussion over server sales.

Friday, June 13, 2008 7:52 AM by JohnBrea

# connect to remote private queues via the COM API

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.

Wednesday, July 30, 2008 1:12 PM by Harley Green

# re: MSMQ and Powershell #1

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.

Thanks

Wednesday, October 08, 2008 2:29 PM by Satyajit

# connect to remote private queues via the COM API

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 :) )

Thanks

Wednesday, October 08, 2008 6:11 PM by JohnBrea

# re: MSMQ and Powershell #1

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

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker