Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting permissions on a MSMQ queue in a script

Can anyone give me some pointers on how to set permissions on MSMQ queues in script, preferably PowerShell, but I'd use VBscript

like image 205
SteveC Avatar asked Apr 19 '09 19:04

SteveC


People also ask

How do I set permissions in MSMQ?

Stop the MSMQ Service (Services -> Message Queuing) Open the C:\WINDOWS\system32\msmq\storage\lqs folder. Find the config file in this folder that describes a queue that has "good" security permissions. You will need to open each file in turn with a text editor to determine which queue it relates to.

How do I create a private queue in MSMQ?

To create a new queue, right click on the desired queue folder (Public Queues or Private Queues) and select New > Public/Private Queue. In the New Public/Private Queue dialog, enter the name of the queue in the Queue name text field. Click OK to confirm.

What is private queue in MSMQ?

Private queues are queues that are not published in Active Directory and are displayed only on the local computer that contains them.

Where can I find MSMQ queues?

Navigate to 'Computer Management (Local) > Services and Applications > Message Queueing > Private Queues' to see the two private queues used by my application.


1 Answers

And here's some example PowerShell including setting the permissions ... sorry about the length

Write-Host ""
Write-Host "Examples using the .NET System.Messaging assembly to access MSMQ"
Write-Host ""

Write-Host "... load the .NET Messaging assembly"
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")

Write-Host ""

if ([System.Messaging.MessageQueue]::Exists(".\private$\MyQueue"))
  {
  [System.Messaging.MessageQueue]::Delete(".\private$\MyQueue")
  Write-Host "... delete old myqueue"
  }
if ([System.Messaging.MessageQueue]::Exists(".\private$\BtsQueue"))
  {
  [System.Messaging.MessageQueue]::Delete(".\private$\BtsQueue")
  Write-Host "... delete old btsqueue"
  }

Write-Host "... create a new queue"
$q1 = [System.Messaging.MessageQueue]::Create(".\private$\MyQueue")

Write-Host "... create new queue, set FullControl permissions for CORP\BIZTALK"
$qb = [System.Messaging.MessageQueue]::Create(".\private$\BtsQueue")

$qb.SetPermissions("CORP\BIZTALK", 
      [System.Messaging.MessageQueueAccessRights]::FullControl,            
      [System.Messaging.AccessControlEntryType]::Set)

Write-Host "... list existing queues" 
$pqs = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine(".")
Write-Host "    Count: "$pqs.length  -ForegroundColor gray
foreach($q in $pqs)
  {
    Write-Host "       "$q.QueueName  -ForegroundColor gray
  }

Write-Host "... access existing queue"
$q2 = New-Object System.Messaging.MessageQueue ".\private$\MyQueue"

Write-Host "... adding string Formatter and additional properties "
$q2.Formatter.TargetTypeNames = ,"System.String"
$q2.MessageReadPropertyFilter.ArrivedTime = $true 
$q2.MessageReadPropertyFilter.SentTime = $true 

Write-Host "... create a new High priorty message "
$msg = New-Object System.Messaging.Message "TestMessage"
$msg.label = "Test Msg Label"
$msg.body = "Add some body to test message"
$msg.priority = [System.Messaging.MessagePriority]::High

Write-Host "... send the High message"
$q2.send($msg)

$msg.body = "Some more text for the test message"
$msg.priority = [System.Messaging.MessagePriority]::Low

Write-Host "... send the Low message"
$q2.send($msg)

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host "... peek at queue"
$ts = New-Object TimeSpan 10000000 # 1 sec. timeout just in case MSMQ is empty
$pk = $q2.Peek($ts)
Write-Host "    ArrivedTime: "$pk.ArrivedTime.DateTime -ForegroundColor gray
Write-Host "    SentTime   : "$pk.SentTime.DateTime -ForegroundColor gray

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length -ForegroundColor gray

Write-Host "... receive from queue"
$rmsg = $q2.receive($ts)
Write-Host "    Body : "$rmsg.body  -ForegroundColor gray
Write-Host "    Label: "$rmsg.label -ForegroundColor gray

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host "... purge the queue "
$q2.Purge()

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host ""
Write-Host "All done, but remember to delete the test queues !!"
like image 154
SteveC Avatar answered Sep 18 '22 00:09

SteveC