Part 1 of a series of posts on working with IBM WebSphere MQ from Windows PowerShell

Windows PowerShell is an object-oriented command line shell and scripting language, available for Windows XP, Windows Vista, and Windows Server 2003.

With the release of SupportPac MO74 (WebSphere MQ – Windows Powershell Library), Windows PowerShell can now be used to administer WebSphere MQ. In the next few posts, I’ll go through a few common WMQ system administration tasks in PowerShell.

For people new to Window PowerShell, I’ve added a few links to good resources for beginners at the end of my post.

Instructions on installing the PowerShell support for WebSphere MQ can be found in the MO74 documentation (pdf). I’ll be writing about what you can do once you’ve done that.

To start with – getting your WMQ objects.

The commands all start with Get-WMQ:


PS C:\> Get-Command Get-WMQ*

  CommandType     Name
  -----------     ----
  Cmdlet          Get-WMQChannel
  Cmdlet          Get-WMQListener
  Cmdlet          Get-WMQNamelist
  Cmdlet          Get-WMQProcess
  Cmdlet          Get-WMQQueue
  Cmdlet          Get-WMQQueueManager
  Cmdlet          Get-WMQService

PS C:\>  

To get queues, you can open PowerShell and type Get-WMQQueue


PS C:\> Get-WMQQueue

...  

I won’t show the output you get here, because it’s pretty big. Try it… you’ll get every property of every queue on every queue manager on your local system.

Remember that PowerShell is an object-oriented shell. Get-WMQQueue is getting you every queue object, and in the absence of any instructions to the contrary, is dumping all of them with all their properties on the console.

You can make this a little more friendly by choosing which attributes you are interested in:


PS C:\> Get-WMQQueue | Select Name, Description, CurrentDepth, MaximumDepth

...  

Get-WMQQueue is still getting all queues, with all their properties, from all local queue managers. But this time, before sending them to the console, we pipe the objects through a Select command which selects the attributes we want – giving us a nice table of results.

This works in the same way for other WMQ objects. To get listeners:


PS C:\> Get-WMQListener | Select Name, TransportType, StartMode, Port

Name                                    TransportType             StartMode             Port
----                                    -------------             ---------             ----
CENTLSTR                                          TCP                  Qmgr             9282
SYSTEM.DEFAULT.LISTENER.LU62                     LU62                Manual
SYSTEM.DEFAULT.LISTENER.NETBIOS               NetBIOS                Manual
SYSTEM.DEFAULT.LISTENER.SPX                       SPX                Manual
SYSTEM.DEFAULT.LISTENER.TCP                       TCP                Manual                0

PS C:\>  

You don’t have to get all objects into the pipeline… so if I wanted to look for listeners beginning with ‘C’:


PS C:\> Get-WMQListener C* | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

This is a short list because I’ve only got one queue manager on my box at the moment. What if I had more than one queue manager, but didn’t want to put objects from all queue managers in the pipeline?

There are so many ways to do this… here are a few:

Use positional parameters – first a listener name, then a queue manager. Get a handle to a queue manager and put it in a variable for use by Get-WMQListener


PS C:\> $myqmgr = Get-WMQQueueManager TESTQM
PS C:\> Get-WMQListener C* $myqmgr | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Use named parameters – but otherwise the same as above


PS C:\> $myqmgr = Get-WMQQueueManager TESTQM
PS C:\> Get-WMQListener -Name C* -Qmgr $myqmgr | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Use positional parameters – first a listener name, then a queue manager. Get the queue manager by including an inner Get-WMQQueueManager command.


PS C:\> Get-WMQListener C* (Get-WMQQueueManager TESTQM) | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Use a named parameter for the queue manager – get the queue manager by including an inner Get-WMQQueueManager command again


PS C:\> Get-WMQListener C* -Qmgr (Get-WMQQueueManager TESTQM) | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Use a named parameter for the queue manager, but this time use the QmgrName parameter, so Get-WMQListener has to get a handle to the queue manager itself


PS C:\> Get-WMQListener C* -QmgrName TESTQM | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Use positional parameters for both listener name and queue manager – again relying on Get-WMQListener to get it’s own handle to the queue manager


PS C:\> Get-WMQListener C* TESTQM | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

Get the queue manager, and pass it down the pipeline to the Get-WMQListener command


PS C:\> Get-WMQQueueManager TESTQM | Get-WMQListener C* | Select Name, TransportType, StartMode, Port

Name              TransportType            StartMode              Port
----              -------------            ---------              ----
CENTLSTR                    TCP                 Qmgr              9282

PS C:\>  

You don’t have to get just one queue manager. All of these examples would work if you used a wildcard to get multiple queue managers.

For example:


PS C:\> Get-WMQListener C* (Get-WMQQueueManager TEST*) | Select Name, TransportType, StartMode, Port

Name                  TransportType            StartMode              Port
----                  -------------            ---------              ----
CENTLSTR                        TCP                 Qmgr              9282
CENT_ON_TESTQM2                 TCP               Manual              9283
   
PS C:\>  

What’s next?

There’s lots more that you can do. Tomorrow, I’ll write about how to create WebSphere MQ objects in PowerShell.

If you can’t wait till then, or want more examples, there is a “Cookbook for ‘PowerShell for WebSphere MQ'” – which contains dozens of worked examples for using PowerShell with your queue managers. Download the MO74 zip file and you will find the powershellcookbook.pdf included.

What do you think?

What do you think about this? Feedback on the SupportPac or the potential of PowerShell administration for WebSphere MQ, is very welcome. If you have any questions, I’d also be happy to try and answer them.

Recommended resources for PowerShell beginners