问题
I googled how to do this and I found this code:
Write-Host "... create a new queue"
$q1 = [System.Messaging.MessageQueue]::Create(".\private$\myqueues")
Write-Host "... create new queue, set FullControl permissions for queuename"
$qb =[System.Messaging.MessageQueue]::Create(".\private$\queuename")
$qb.SetPermissions("queuename",
[System.Messaging.MessageQueueAccessRights]::FullControl,
[System.Messaging.AccessControlEntryType]::Set)
but when I run it, I get this error:
Unable to find type [System.Messaging.MessageQueueAccessRights]. Make sure
that the assembly that contains this type is loaded.
At line:7 char:1
+ $qb.SetPermissions("hazeljob",
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation:(System.Messagin...eueAccessRi
ghts:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
I thought it might be because Message Queuing wasn't installed but then I installed it and still got this error.
Why is this happening? Is this code not compatible with Windows Server 2008 R2? I input my company values over the ones that were in there originally.
回答1:
You seem to have two problems with your code.
First, you need to tell PowerShell to import the System.Messaging
namespace code from the Global Assembly Cache:
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
Next, when using SetPermission
on a MessageQueue
instance, you don't need to supply the queue name again - it already knows who it is, so to speak.
What you do need to supply is the username of the user, security group or computer to whom you want to grant access to the message queue.
So, if you wanted to grant your own user account FullControll access on the queue "MyQueue", and your username is "david.wilson", that becomes:
$Queue = [System.Messaging.MessageQueue]::Create(".\private$\MyQueue")
$Queue.SetPermissions("david.wilson",[System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Set)
来源:https://stackoverflow.com/questions/33807521/creating-private-queues-in-powershell-on-windows-server-2008-r2