Set MSMQ permissions using VBScript (via Nant)
We are using Nant to deploy an application and need to create some MSMQ Queues during the process.
We use Nant/psexec/cscript to call a VBScript to create the queue itself but we also need to set the permissions.
Can this be done programmatically?
I am aware of Setting permissi开发者_运维问答ons on a MSMQ queue in script however that question asks for PowerShell or VBScript and has an accepted answer for PowerShell. We do not have PowerShell available to us so this question is specific to VBScript.
According to this post, MSMQ scripting API doesn't provide any means to set permissions on queries, so this task cannot be accomplished using VBScript.
I can suggest the following workarounds:
- Use PowerShell, as illustrated in the post you linked to. (PowerShell can use the .NET Framework and, therefore, the MSMQ .NET API provided by the
System.Messaging
assembly.) - Create a custom utility that would set the necessary permissions and call this utility as part of your deployment process. For setting the queue security, you can use the following API functions:
MQSetQueueSecurity
(in C or C++)MessageQueue.SetPermissions
(in .NET)
Just came across this and saw that it was unanswered. The trick here is to use VB.Net or C# code directly in nAnt. You can do this with a nAnt include, with a script task in the global space; and in the code, derive from the Task class.
Or. you can put your script block in a target. I prefer the former method, since it is easier to reuse and pass in parameters.
In either case, this is NOT VBScript, it is fully compiled .Net code, so you can do pretty much anything.
In your code element, Import System.Messaging, and create the Queue in your ExecuteTask override. Don't forget to give read/write/peek permissions to .\Users, or you probably don't be able to use the Queue properly except from the user that ran the nAnt script (which usually isn't the web server or Windows service that is reading the queue.)
Also, since you have full access to the Messaging.MessageQueue class, you can check for existence and delete an existing queue, or not!
精彩评论