Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PowerShell

PowerShell: Create MSMQ

5.00/5 (3 votes)
28 Oct 2014CPOL 20.3K  
PowerShell: Create MSMQ

AT work, I use MSMQ a fair bit, and I also use NServiceBus a fair bit of late, which thankfully takes care of creating all the queues needed. But, for those times when you really need to make a bunch of queues, it can be a time consuming exercise, so I decided to see if my new found PowerShell skills could automate this process (remember I am still learning, so this may not be the best/most current way, in fact I know for Windows 8.1/Windows Server, there is a later more groovy API. But for now, this is what I came up with:

C#
[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True,Position=1)]
   [string]$queueName,
 
   [Parameter(Mandatory=$True,Position=2)]
   [bool]$isTransactional,
 
   [Parameter(Mandatory=$True,Position=3)]
   [bool]$isJournalEnabled,
 
   [Parameter(Mandatory=$True,Position=4)]
   [string]$userName,
 
   [Parameter(Mandatory=$True,Position=5)]
   [bool]$isAdminUser
)
 
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
 
function printUsage() 
{
    Write-Host "Usage is CreateQueue.ps1 -queueName SomeQueuName 
        -isTransactional $true -isJournalEnabled $true 
        -userName mclocal\barbers -isAdminUser $true"
} 
 
try {
 
    $fullQueueName = ".\private$\" + $queueName
 
 
    If ([System.Messaging.MessageQueue]::Exists($fullQueueName))
    {
        Write-Host($fullQueueName + " queue already exists")
    }
    else
    {
        $newQ = [System.Messaging.MessageQueue]::Create($fullQueueName, 
            $isTransactional)
        if ($isJournalEnabled)
        { 
            $newQ.UseJournalQueue = $True
        }         
        
        if ($isAdminUser)
        { 
            Write-Host("ADMIN")
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::FullControl, 
                    [System.Messaging.AccessControlEntryType]::Allow)        
        }
        else
        { 
            Write-Host("NOT ADMIN")
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::GenericWrite, 
                [System.Messaging.AccessControlEntryType]::Allow)
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::PeekMessage, 
                [System.Messaging.AccessControlEntryType]::Allow)
            $newQ.SetPermissions($userName, 
                [System.Messaging.MessageQueueAccessRights]::ReceiveJournalMessage, 
                [System.Messaging.AccessControlEntryType]::Allow)
        }
    }
}
catch [Exception] {
   Write-Host $_.Exception.ToString()
  printUsage
}

This will allow you to create a private queue of your choice of name, where you can also pick the following:

  • Whether it is a transactional queue
  • If journaling is enabled
  • If it is an admin user queue

Hope it helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)