We can manage MSMQ using Powershell. Powershell scripts help you automate the management of MSMQ and provide a quick way to create queues, manage users and permissions, get queue information etc. We have seen several blogs on internet, but here we will try to provide a collective information related to MSMQ management.

In this blog we will discuss how to implement System.Messaging.MessageQueue to implement various MSMQ functionalities. We will cover following topics:

  1. Creating Queues
       Private/Public
       Transactional/NonTransactional
       Journal Queues
  2. Adding user to the private queues and setting permissions
       Full Permission
       Restricted Permission
  3. Deleting the Queues

I am uploading a powershell script which will implement all the mentioned functionalities. I will discuss the implementation of each of these functionalities today.
To run any powershell script, please run the following command: set-executionpolicy unrestricted
In order to create MSMQ, we need to implement the System.Messaging.MessageQueue.

[Reflection.Assembly]::LoadWithPArtialName("System.Messaging")
$msmq = [System.Messaging.MessageQueue]

Creating MSMQ Queues

Feed the input: Enter Queuename, Y/N - Private, Y/N - Transactional, Y/N - Journal (separate with comma)

Get each value in a separate variable and then on the basis of whether the queue is private or not, form the Queue Path:

$name = $parameters[0]
$isPrivate = $parameters[1]
$isTransactional = $parameters[2]
$isJournal = $parameters[3] 

Before creating the Queue, we should check if the queue already exists:

If ($msmq::Exists($qname))
{
                  Echo ($name + " queue already exists")
}

For example, I have a private queue “testprivps” already created, then the script should return that the queue is already existing.

If the queue does not exists, then proceed with creating the queues. On the basis of whether the queue is private/public, create the path.

If ($isPrivate -ieq "Y")
      {
            $qname = ".\private$\" + $name 

If Public Queue: $qname = ".\" + $name 

To declare the queue as Transactional/Non Transactional, following line of code has to be written: 

If ($isTransactional -ieq "Y")
{
$qObject = $msmq::Create($qname,1) (1 indicates Transactional Queue)
}
Else
{
$qObject = $msmq::Create($qname)
}

To declare the queue as Journal, following line of code has to be written:

If ($isJournal -ieq "Y")
{
$qObject.UseJournalQueue = $TRUE
}

For e.g. if I create a private transactional queue, with name: testprivps and journaling enabled, give the following parameters as input:


The reason why the Queue got created with a name which is not case sensitive is a fact that for MSMQ, TestPrivPS and testprivps is the same thing. Refer to the following article for more details: http://msdn.microsoft.com/en-us/library/windows/desktop/ms706083(v=vs.85).aspx 

Adding user to the private queues and setting permissions

Permissions can be granted at 2 levels:
   Full Permission
   Restricted Permission 

Following statement can be used to get the input about the user name and the permission level:

$Control = (Read-Host "Enter Username, Full Control <Y/N> (separate with comma)").split (',') | % {$_.trim ()}

System.Messaging.MessageQueueAccessRights  is used to set the permission type, whereas System.Messaging.AccessControlEntryType is used to define if the permission is given or denied.

For full control, we need to write the following line of code:

$qObject.SetPermissions($user, [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Allow)

To set the permissions that are granted to the supplied user account or group, we need to use the following keys corresponding to the permission type: 

Delete (Delete the specified queue) - DeleteQueue
Receive (Receive a message from the specified queue) - ReceiveMessage
Peek (Peek a message from the specified queue) - PeekMessage
JournalReceive (Receive a message from the specified queue’s journal queue) - ReceiveJournalMessage
GetProperties (Get the properties of the specified queue) - GetQueueProperties
SetProperties (Set the properties of the specified queue) - SetQueueProperties
GetPermissions (Get the permissions of the specified queue) - GetQueuePermissions
SetPermissions (Set the permissions of the specified queue) - ChangeQueuePermissions
TakeOwnership (Assign the specified queue to oneself) - TakeQueueOwnership
Send (send a message to the specified queue) - WriteMessage

For e.g.
$qObject.SetPermissions($user, [System.Messaging.MessageQueueAccessRights]::DeleteQueue, [System.Messaging.AccessControlEntryType]::Set)

$qObject.SetPermissions($user, [System.Messaging.MessageQueueAccessRights]::ReceiveMessage, [System.Messaging.AccessControlEntryType]::Allow)

$qObject.SetPermissions($user, [System.Messaging.MessageQueueAccessRights]::PeekMessage, [System.Messaging.AccessControlEntryType]::Allow)

 


For Restricted Permissions :

 

Delete the Queues

To delete the queue, we need to determine if the queue that we want to delete is a private or a public queue. The Queue deletion will depend on the queue path.

On the basis of whether the queue is private or public, we will create the path accordingly and delete the queue.

[System.Messaging.MessageQueue]::Delete($qname) 

You can use the following piece of code for deleting a private queue:
$QueueName = Read-Host 'Enter the queue name you want to delete'
      $qname = ".\private$\" + $QueueName
      if ($msmq::Exists($qname)) 
      { 
            [System.Messaging.MessageQueue]::Delete($qname)

For example:

Please note that in this script we have not used any code to handle the errors. In the upcoming blogs, we will discuss the implementation of managing messages in the MSMQ Queues. Also, if we are using Powershell Extensions or Windows Server 2012, then we can use Powershell commandlets to manage the MSMQ Queues. We will also discuss the use of these cmndlets in some time.

 

Hope this helps.. !!

Written By
Rasika Chaudhary

Reviewed By
Gautam Mansinghka 

Microsoft GTSC