This blog shows you how to How to Add and Delete MSMQ Queues from Powershell, programmatically, via a simple re-usable function.
I used the code from this blog as a base: http://blogs.msdn.com/b/sajay/archive/2010/03/18/powershell-script-to-create-an-msmq.aspx. His code is fine, but it’s from an administrators perspective, i.e. run this Powershell, pass it some parms. I wanted the same code to be callable as a function that could be called from other Powershell code. Another source of similar information is here: http://blogs.msdn.com/b/biztalknotes/archive/2013/08/19/powershell-scripts-to-manage-msmq.aspx
In earlier blogs, I demonstrated How to Write to MSMQ from Powershell and How to Read MSMQ from Powershell. Those dealt with read and writing individual queue messages from an existing queue in MSMQ. In this blog, the topic is creating and removing the entire queue in MSMQ.
function CreateMSMQQueue($queuename, $YNPrivate, $YNTransactional, $user, $permission) { if ($permission -ne "all" -and $permission -ne "restricted") { Write-Host "Error: fifth parameter "permission" must have value 'all' or 'restricted'" exit } [Reflection.Assembly]::LoadWithPartialName("System.Messaging") if ($YNPrivate -ieq "Y") { $queuename = ".\private$\" + $queuename } if ($YNTransactional -ieq "Y") { $transactional = 1 Write-Host "Creating a transactional queue." } else { $transactional = 0 Write-Host "Creating a non-trasactional queue" } $msgQueue = [System.Messaging.MessageQueue]::Create($queuename, $transactional) if($msgQueue -eq $null) { Write-Host "Error: got a 'null' back from the Create MSMQ function" exit } $msgQueue.label = $queuename if ($permission -ieq "all") { Write-Host "Granting all permissions to " $User $msgQueue.SetPermissions($User, [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Allow) } else { Write-Host "Restricted Control for user: " $User Write-Host "" $msgQueue.SetPermissions($User, [System.Messaging.MessageQueueAccessRights]::DeleteMessage, [System.Messaging.AccessControlEntryType]::Set) $msgQueue.SetPermissions($User, [System.Messaging.MessageQueueAccessRights]::GenericWrite, [System.Messaging.AccessControlEntryType]::Allow) $msgQueue.SetPermissions($User, [System.Messaging.MessageQueueAccessRights]::PeekMessage, [System.Messaging.AccessControlEntryType]::Allow) $msgQueue.SetPermissions($User, [System.Messaging.MessageQueueAccessRights]::ReceiveJournalMessage, [System.Messaging.AccessControlEntryType]::Allow) } } function DeleteMSMQQueue($queuename, $YNPrivate) { Write-Host "DeleteMSMQQUEUE QueueName: " + $queuename if ($YNPrivate -ieq "Y") { $queuename = ".\private$\" + $queuename } Write-Host "Delete Queue: " + $queuename [System.Messaging.MessageQueue]::Delete($queuename) } cls $myQueueName = "NealTestPSMSMQ1" $ynIsTransactional = "Y" $ynIsPrivate = "Y" $user ="Administrator" $permission = "all" CreateMSMQQueue $myQueueName $ynIsPrivate $ynIsTransactional $user $permission #use the following instead of Create to Delete the Queue #DeleteMSMQQueue $myQueueName $ynIsPrivate
Results
To view the results, open “Server Manager”, expand “Features” and “Message Queueing”, and look for the queue just created. In my example above, the queue name was ‘nealtestpsmsmq1’.
To read or write individual messages to that queue, see previous blogs (links at top of this article).
One potential enhancement to the above code might be to pass an array or collection of userids and desired privileges to the function, or to create a third function to manage the security privileges for the queue.
In summary, this article has shown how to create and remove a MSMQ queue using Powershell.