Powershellcookbook
Powershellcookbook
Examples for
PowerShell for WebSphere MQ
A collection of one-liners demonstrating the sorts of operations possible with PowerShell
administration for WebSphere MQ.
1 of 28 15/01/2008
Table of Contents
get a list of local queue managers........................................................................................................................................................................................3
show local queue managers which have names which end in 'st'........................................................................................................................................3
search for an object across multiple queue managers..........................................................................................................................................................4
getting a subset of objects across a subset of queue managers............................................................................................................................................4
find queues which are getting full and increase allocated space..........................................................................................................................................5
count the number of queues on your system........................................................................................................................................................................5
find transmission queues and their usage.............................................................................................................................................................................6
get a list of SSL-enabled channels.......................................................................................................................................................................................6
get details about all sender channels from a subset of queue managers..............................................................................................................................7
generating HTML reports....................................................................................................................................................................................................7
generating CSV spreadsheets...............................................................................................................................................................................................8
using constants.....................................................................................................................................................................................................................8
equivalent approaches to pipelining...................................................................................................................................................................................12
requiring confirmation.......................................................................................................................................................................................................13
comparing current state with a previous known good state ..............................................................................................................................................16
delete all non-system objects ............................................................................................................................................................................................17
modify running objects......................................................................................................................................................................................................17
sending messages ..............................................................................................................................................................................................................18
receiving messages ............................................................................................................................................................................................................19
sending the string contents of a text file ............................................................................................................................................................................19
receiving string message data into a text file ....................................................................................................................................................................19
getting less information by default.....................................................................................................................................................................................20
adding additional properties to WebSphere MQ objects...................................................................................................................................................23
getting the SSL properties of all queue managers..............................................................................................................................................................24
check that queue managers have the right SSL key repository file...................................................................................................................................25
check that all channels have valid transmission queues.....................................................................................................................................................25
getting remote queue managers..........................................................................................................................................................................................26
specifying a subset of known remote queue managers......................................................................................................................................................26
getting properties of remote queue managers....................................................................................................................................................................27
getting remote queue manager info from WebSphere MQ Explorer.................................................................................................................................27
script multiple profiles for WebSphere MQ Explorer........................................................................................................................................................28
getting queues from UNIX queue managers only..............................................................................................................................................................28
2 of 28 15/01/2008
get a list of local queue managers
reverse sort the list by name, showing the name, ID and description
show local queue managers which have names which end in 'st'
Name QueueManagerIdentifier
---- ----------------------
post post_2007-09-03_09.34.19
test test_2007-09-02_22.14.03
Test Test_2007-09-03_09.33.44
3 of 28 15/01/2008
search for an object across multiple queue managers
get a list of queues which contain the word "CLUSTER" in their name, from queue managers with names ending in "st"
Name Qmgr
---- ----
SYSTEM.CLUSTER.COMMAND.QUEUE post
SYSTEM.CLUSTER.REPOSITORY.QUEUE post
SYSTEM.CLUSTER.TRANSMIT.QUEUE post
SYSTEM.CLUSTER.COMMAND.QUEUE test
SYSTEM.CLUSTER.REPOSITORY.QUEUE test
SYSTEM.CLUSTER.TRANSMIT.QUEUE test
SYSTEM.CLUSTER.COMMAND.QUEUE Test
SYSTEM.CLUSTER.REPOSITORY.QUEUE Test
SYSTEM.CLUSTER.TRANSMIT.QUEUE Test
4 of 28 15/01/2008
find queues which are getting full and increase allocated space
Show all local queues on all local queue managers where the current queue depth is less than 10 messages away from it's max depth setting
PS C:\> Get-WMQQueue | Where {$_.QueueType -eq “Local” -and $_.CurrentDepth -gt ($_.MaximumDepth - 10)}
| Select Name, CurrentDepth, MaximumDepth
And if you want to increase the maximum depth of these queues – such as to add space for 10 more messages…
PS C:\> Get-WMQQueue | Where {$_.QueueType -eq “Local” -and $_.CurrentDepth -gt ($_.MaximumDepth - 10)}
| foreach {Set-WMQQueue $_ -MaximumDepth ($_.MaximumDepth + 10)}
Count all queues on your system. Or just count local queues. Or queues matching any criteria you are interested in.
Count : 25
5 of 28 15/01/2008
find transmission queues and their usage
get all transmission queues (except the cluster transmit queue) from all queue managers and show their depths and open counts
get all channels from all local queue managers which have an SSL Cipher Spec applied, and show their name, sslciph, conname and the name of the queue
manager they are on - sorted by channel name
6 of 28 15/01/2008
get details about all sender channels from a subset of queue managers
get all non-system (i.e. channels with names that don't start with SYSTEM) sender channels from local queue managers with names ending in "st", and show
their name, conname, transmit queue, sslciph, and the name of the queue manager they are on
PS C:\> Get-WMQChannel * *st | Where {$_.Name -notlike "SYSTEM.*" -and $_.ChannelType -eq “Sender”} | Select Name, ConnectionName,
TransmissionQueueName, SSLCipherSpec, @{e={$_.QueueManager.Name};n='Hosting Queue Manager'}
generate an HTML webpage with a table showing the name, description and depth information for all queues on the 'test' queue manager, and open this
HTML file in a web-browser
7 of 28 15/01/2008
generating CSV spreadsheets
generate a CSV spreadsheet containing the name, description and depth information for all queues on the 'test' queue manager, and open this in Excel
PS C:\> Get-WMQQueue * test | Select Name, Description, CurrentDepth, MaximumDepth | Export-Csv -path
myqueues.csv
PS C:\> Invoke-Item myqueues.csv
using constants
function Get-EnumValues{
[enum]::getvalues($args[0]) | select @{n="Name";e={$_}},@{n="Value";e={$_.value__}} | ft -auto
}
PS C:\> Get-EnumValues([WebSphereMQ.MQC+InhibitGetTypes])
Name Value
---- -----
Allowed 0
Inhibited 1
get a queue...
InhibitGet : Allowed
8 of 28 15/01/2008
ProcessName :
MaximumDepth : 5000
MaximumMessageLength : 4194304
BackoutThreshold : 0
BackoutRequeueName :
Shareability : Shareable
DefaultInputOpenOption : Shared
HardenGetBackout : Hardened
MessageDeliverySequence : 0
RetentionInterval : 999999999
DefinitionType : Predefined
Usage : Normal
OpenInputCount : 0
OpenOutputCount : 0
CurrentDepth : 0
CreationDateTime : 02/12/2007 12:32:29
InitiationQueueName :
TriggerControl : Off
TriggerType : First
TriggerMessagePriority : 0
TriggerDepth : 1
TriggerData :
Scope : Qmgr
DepthHighEvent : 0
DepthHighLimit : 80
DepthLowEvent : 0
DepthLowLimit : 20
DepthMaximumEvent : 1
ServiceInterval : 999999999
ServiceIntervalEvent : None
ClusterName :
ClusterNamelist :
DefaultBind : OnOpen
ClusterWorkLoadRank : 0
ClusterWorkLoadPriority : 0
ClusterWorkLoadUseQ : AsQmgr
TPIPE :
QueueAccounting : Qmgr
QueueMonitoring : Qmgr
QueueStatistics : Qmgr
NonPersistentMessageClass : Normal
PagesetId : 0
QueueManager : WebSphereMQ.MQQueueManager
Name : SYSTEM.DEFAULT.LOCAL.QUEUE
QueueType : Local
Description :
InhibitPut : Allowed
DefaultMessagePriority : 0
DefaultMessagePersistence : NotPersistent
AlterationDateTime : 02/12/2007 12:32:29
9 of 28 15/01/2008
I can see that I’m allowed to GET from it. I want to stop that...
Using Get-Member
TypeName: WebSphereMQ.MQLocalQueue
Seeing the '+' symbol tells me the type is an enum called InhibitGetTypes in the WebSphereMQ.MQC class.
PS C:\> Get-EnumValues([WebSphereMQ.MQC+InhibitGetTypes])
Name Value
---- -----
Allowed 0
Inhibited 1
10 of 28 15/01/2008
This tells me I can either just set InhibitGet to 1 ...
Name InhibitGet
---- ----------
SYSTEM.DEFAULT.LOCAL.QUEUE Inhibited
or be a little more verbose and readable by using the enum name ...
Name InhibitGet
---- ----------
SYSTEM.DEFAULT.LOCAL.QUEUE Inhibited
Note that the absolute name of the enum is not required. If you leave it out, it is implicitly added...
Name InhibitGet
---- ----------
SYSTEM.DEFAULT.LOCAL.QUEUE Inhibited
11 of 28 15/01/2008
Relying on feedback
Alternatively, I could always have been lazy and made an intentional error - and used the error message feedback to guide me...
Finally, if you are used to the standard IBM constant names, the implementation of WebSphere MQ for PowerShell is such that it uses the same values
Name InhibitGet
---- ----------
SYSTEM.DEFAULT.LOCAL.QUEUE Inhibited
12 of 28 15/01/2008
…similarly, if you want to use a couple of lines…
requiring confirmation
Commands which result in a change to WMQ objects have a confirm level of MEDIUM.
If your ConfirmPreference is higher than this, you will not be prompted for confirmation:
Name Value
---- -----
ConfirmPreference High
Name InhibitPut
---- ----------
TESTQ Allowed
13 of 28 15/01/2008
If your ConfirmPreference is equal to or lower than this, you will be prompted for confirmation:
Name InhibitPut
---- ----------
TESTQ Inhibited
If you already have ConfirmPreference set to low/medium, then you can override the prompt for confirmation:
Name Value
---- -----
ConfirmPreference Medium
Name InhibitPut
---- ----------
TESTQ Allowed
14 of 28 15/01/2008
If you already have ConfirmPreference set to high, then you can override the need for confirmation:
Name Value
---- -----
ConfirmPreference High
Name InhibitPut
---- ----------
TESTQ Inhibited
15 of 28 15/01/2008
comparing current state with a previous known good state
Store information about all queues which have a "CurrentDepth" value in a CSV file
16 of 28 15/01/2008
delete all non-system objects
PS C:\> Get-WMQListener LISTR TESTQM | Stop-WMQListener -PassThru | Set-WMQListener -Port 9999 -PassThru
| Start-WMQListener
These commands should work in theory, but in practice they will likely fail – as Stop and Start cmdlets do not wait for the operations to complete before
returning. This will be addressed with a –Wait parameter in a future version.
17 of 28 15/01/2008
sending messages
PS C:\>
PS C:\> Get-WMQQueue FIS* | Select Name, CurrentDepth
Name CurrentDepth
---- ------------
FISH 0
FISH2 0
FISH3 0
Name CurrentDepth
---- ------------
FISH 2
FISH2 2
FISH3 2
18 of 28 15/01/2008
receiving messages
or in one line:
19 of 28 15/01/2008
getting less information by default
Get commands display all attributes of all objects by default – which can be a lot of information. This can be reduced to just the fields you are interested in.
Instead of displaying all attributes for each queue, what if you are normally only interested in names, queue type and description? This can be done with a
type data file.
<Types>
<Type>
<Name>WebSphereMQ.MQQueue</Name>
<Members>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<NoteProperty>
<Name>DefaultDisplayProperty</Name>
<Value>ReferencedPropertyName</Value>
</NoteProperty>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>Name</Name>
<Name>QueueType</Name>
<Name>Description</Name>
</ReferencedProperties>
</PropertySet>
<PropertySet>
<Name>DefaultKeyPropertySet</Name>
<ReferencedProperties>
<Name>Name</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
</Members>
</Type>
</Types>
20 of 28 15/01/2008
Then in a PowerShell window, update the type data with the information in the file:
After this, WebSphereMQ.MQQueue objects will be displayed in a table with name, queue type and description:
PS C:\> Get-WMQQueue
21 of 28 15/01/2008
SYSTEM.MQSC.REPLY.QUEUE Model WebSphere MQ MQSC Reply Queue
SYSTEM.PENDING.DATA.QUEUE Local WebSphere MQ Deferred Message Queue
TESTQ Local
XMITQ Local
The other properties are still accessible – just not displayed by default. Pipe the output from a Get-WMQQueue command to a Select command identifying
the properties to display for individual commands.
22 of 28 15/01/2008
adding additional properties to WebSphere MQ objects
Name Qmgr
---- ----
SYSTEM.DEFAULT.LOCAL.QUEUE APPSQM
SYSTEM.DEFAULT.LOCAL.QUEUE SANDBOX
SYSTEM.DEFAULT.LOCAL.QUEUE TESTQMGR
Getting the name of the queue manager hosting an object can be made easier by adding a custom hostqmgrname property to objects.
This can be done with a type data file. Create a file called WebSphereMQ.Types.ps1xml
<Types>
<Type>
<Name>WebSphereMQ.MQQueue</Name>
<Members>
<ScriptProperty>
<Name>HostQmgrName</Name>
<GetScriptBlock>$this.QueueManager.Name</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>
Then in a PowerShell window, update the type data with the information in the file:
23 of 28 15/01/2008
PS C:\> Get-WMQQueue SYSTEM.DEFAULT.LOCAL.QUEUE | Select Name, @{e={$_.QueueManager.Name};n='Qmgr'}, HostQmgrName
Name : APPSQM
SSLCRLNamelist :
SSLCryptoHardware :
SSLEvent : Disabled
SSLFips : No
SSLKeyRepository : C:\Program Files\IBM\WebSphere MQ\qmgrs\APPSQM\ssl\key
SSLKeyResetCount : 0
SSLTasks : 0
Name : qmgr1fish
SSLCRLNamelist :
SSLCryptoHardware :
SSLEvent : Disabled
SSLFips : No
SSLKeyRepository : C:\Program Files\IBM\WebSphere MQ\qmgrs\qmgr1fish\ssl\key
SSLKeyResetCount : 0
SSLTasks : 0
24 of 28 15/01/2008
check that queue managers have the right SSL key repository file
get the SSL key repository property from the queue manager, and check that a file exists at that location (appending the correct file extension)
Name Exists
---- ------
APPSQM False
qmgr1SSL True
qmgr2SSL True
SANDBOX False
TESTQMGR False
for every channel, check that it’s transmission queue name refers to an actual queue with usage set to Transmission and InhibitPut set to Allowed
PS C:\> Get-WMQChannel -QmgrName qmgr1test | Where {$_.Name -notlike "SYSTEM.*" } | Select Name,
TransmissionQueueName, @{e={ (Get-WMQQueue -qmgrname qmgr1test -Name ($_.TransmissionQueueName) | Where
{$_.Usage -eq “Transmission” -and $_.InhibitPut -eq “Allowed”}) -ne $null }; n='Valid transmission queue'}
25 of 28 15/01/2008
In reality, this is stretching how much you would want to do in a single line – as it has lost the readability benefits that PowerShell should bring. This could
be expanded into a function, making it easier to read and maintain.
However, once you get used to using PowerShell, it is possible to write lines like this interactively. And it shows how powerful the sort of queries you can
write can be.
specify connection information to remote queue managers, and use that to include remote queue managers in the objects returned by the Get-
WMQQueueManager
These queue manager objects can then be used in the same way as local queue managers.
26 of 28 15/01/2008
getting properties of remote queue managers
get objects from remote queue managers, and show them together with properties of the queue manager that they are on
if you’ve already taken the time to specify the connection details for your remote queue managers in WebSphere MQ Explorer (v6), why not reuse that?
Note that the exact path will vary depending on installation and user.
27 of 28 15/01/2008
script multiple profiles for WebSphere MQ Explorer
the reverse is also possible – allowing PowerShell to be used to script the configuring of WebSphere MQ Explorer connections
Note that the exact path will vary depending on installation and user.
Further note that WebSphere MQ Explorer reads this file on start-up, so Explorer should be stopped before exporting.
28 of 28 15/01/2008