Setting permissions on a MSMQ queue in a script
Asked Answered
A

4

17

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

Anthracosilicosis answered 19/4, 2009 at 19:38 Comment(0)
R
17

A present (2015) day answer to this question.

Example:

New-MsmqQueue -Name "MyQueue" -QueueType Private
Get-MsmqQueue -Name "MyQueue" -QueueType Private | 
  Set-MsmqQueueAcl -UserName "Everyone" -Allow FullControl

Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2

Reference: https://technet.microsoft.com/en-us/library/dn391735(v=wps.630).aspx

Rawdin answered 18/12, 2015 at 12:16 Comment(2)
instead of the Get-MsmqQueue you could also $newQueue = New-MsmqQueue... and then $newQueue | Set-MsmqQueueAcl...Bradawl
See also: #43597266 Documentation is outdated as of May 2019Chest
A
29

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 !!"
Anthracosilicosis answered 27/5, 2009 at 10:59 Comment(2)
This is now obsolete - @Davids answer is much better.Beaker
This is a great answer for those who are running Windows 7 or pre-Windows Server 2012 R2. Otherwise, @GregSansom is correct. Thanks for posting this answer.Perryperryman
R
17

A present (2015) day answer to this question.

Example:

New-MsmqQueue -Name "MyQueue" -QueueType Private
Get-MsmqQueue -Name "MyQueue" -QueueType Private | 
  Set-MsmqQueueAcl -UserName "Everyone" -Allow FullControl

Applies To: Windows 8.1, Windows PowerShell 4.0, Windows Server 2012 R2

Reference: https://technet.microsoft.com/en-us/library/dn391735(v=wps.630).aspx

Rawdin answered 18/12, 2015 at 12:16 Comment(2)
instead of the Get-MsmqQueue you could also $newQueue = New-MsmqQueue... and then $newQueue | Set-MsmqQueueAcl...Bradawl
See also: #43597266 Documentation is outdated as of May 2019Chest
W
2

There's nothing built into powershell for this, but you can use the .NET framework classes. Just load System.Messaging.dll and use MessageQueue.SetPermissions() to change the ACL on the queue.

Westleigh answered 23/4, 2009 at 23:13 Comment(0)
C
2

Windows 2012 and 8.1 now have MSMQ cmdlets. The one to set the permissions on a queue is: Set-MsmqQueueAcl

Caesarism answered 17/4, 2015 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.