How can I programatically set the owner of a MSMQ queue? How can I programatically set the owner of a MSMQ queue? powershell powershell

How can I programatically set the owner of a MSMQ queue?


I know this is an ancient question and has already been answered but I was struggling with this for far too long today and have to post my solution to spare others the pain.

If you don't have access to the MSMQ by default then you need to run the commands as an impersonated user as @Noon Silk suggested.

This code will let you create and assign permissions to the queues as a different user

$Username = "Eric"$Password = "MyPassword"$securePass = ConvertTo-SecureString $Password -AsPlainText -Force$credential = New-Object Management.Automation.PSCredential($Username, $securePass)foreach($strQueue in $strQueues) {  if (![System.Messaging.MessageQueue]::Exists($strQueue)) {    $script =         {            $q = [System.Messaging.MessageQueue]::Create($strQueue)            $q.SetPermissions($queueUser, [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Set)            $q.SetPermissions("BUILTIN\Administrators", [System.Messaging.MessageQueueAccessRights]::TakeQueueOwnership, [System.Messaging.AccessControlEntryType]::Set)            Write-Host "... created $strQueue and set FullControl permissions for $queueUser"        }    Invoke-Command -Credential $credential -ScriptBlock $script  }}


I think that taking ownership can be done only from native code ( with the c api of msmq). So no powershell here. But there is a c+ sample here.


You could try impersonating a relevant admin account before creating them ... ?