在脚本中设置 MSMQ 队列的权限
Posted
技术标签:
【中文标题】在脚本中设置 MSMQ 队列的权限【英文标题】:Setting permissions on a MSMQ queue in a script 【发布时间】:2010-10-20 10:32:53 【问题描述】:谁能给我一些关于如何在脚本中设置 MSMQ 队列权限的指示,最好是 PowerShell,但我会使用 VBscript
【问题讨论】:
【参考方案1】:这个问题的当前(2015 年)答案。
例子:
New-MsmqQueue -Name "MyQueue" -QueueType Private
Get-MsmqQueue -Name "MyQueue" -QueueType Private |
Set-MsmqQueueAcl -UserName "Everyone" -Allow FullControl
适用于:Windows 8.1、Windows PowerShell 4.0、Windows Server 2012 R2
参考:https://technet.microsoft.com/en-us/library/dn391735(v=wps.630).aspx
【讨论】:
代替Get-MsmqQueue
你也可以$newQueue = New-MsmqQueue...
然后$newQueue | Set-MsmqQueueAcl...
另请参阅:***.com/questions/43596766/… 文档已于 2019 年 5 月过时【参考方案2】:
Windows 2012 和 8.1 现在具有 MSMQ cmdlet。设置队列权限的方法是:Set-MsmqQueueAcl
【讨论】:
【参考方案3】:这里有一些示例 PowerShell,包括设置权限... 对不起,长度
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 !!"
【讨论】:
这已经过时了 - @Davids 的回答要好得多。 对于运行 Windows 7 或 Windows Server 2012 R2 之前版本的用户来说,这是一个很好的答案。否则,@GregSansom 是正确的。感谢您发布此答案。【参考方案4】:powershell 没有为此内置任何内容,但您可以使用 .NET 框架类。只需加载 System.Messaging.dll 并使用 MessageQueue.SetPermissions() 更改队列上的 ACL。
【讨论】:
以上是关于在脚本中设置 MSMQ 队列的权限的主要内容,如果未能解决你的问题,请参考以下文章
队列不存在或您没有足够的权限来执行该操作。通过 MSMQ 发送消息时出现异常