在 Windows Server 2008 R2 上的 PowerShell 中创建私有队列
Creating private queues in PowerShell on Windows Server 2008 R2
我用谷歌搜索了如何执行此操作,并找到了这段代码:
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)
但是当我 运行 它时,我得到这个错误:
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
我认为可能是因为没有安装消息队列,但后来我安装了它,仍然出现这个错误。
为什么会这样?此代码与 Windows Server 2008 R2 不兼容吗?我输入了我的公司价值观,而不是原来的价值观。
您的代码似乎有两个问题。
首先,您需要告诉 PowerShell 从全局程序集缓存中导入 System.Messaging
命名空间代码:
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
接下来,在 MessageQueue
实例上使用 SetPermission
时,您不需要再次提供队列名称 - 它已经知道它是谁了,可以这么说。
您做的需要提供的是您要向其授予消息队列访问权限的用户、安全组或计算机的用户名。
因此,如果您想授予自己的用户帐户对队列 "MyQueue" 的 FullControll 访问权限,并且您的用户名是 "david.wilson",则变为:
$Queue = [System.Messaging.MessageQueue]::Create(".\private$\MyQueue")
$Queue.SetPermissions("david.wilson",[System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Set)
希望此时 Server 2008 实例已经或正在停用,但无论如何您也可以使用 Powershell 5 创建队列。
function CreateQueue(
[string] $queueName,
[string] $queueType = 'Private'
) {
New-MsmqQueue -Name $queueName -QueueType $queueType
}
CreateQueue -queueName 'PrivateTestQueue'
https://docs.microsoft.com/en-us/powershell/module/msmq/new-msmqqueue?view=win10-ps
我用谷歌搜索了如何执行此操作,并找到了这段代码:
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)
但是当我 运行 它时,我得到这个错误:
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
我认为可能是因为没有安装消息队列,但后来我安装了它,仍然出现这个错误。
为什么会这样?此代码与 Windows Server 2008 R2 不兼容吗?我输入了我的公司价值观,而不是原来的价值观。
您的代码似乎有两个问题。
首先,您需要告诉 PowerShell 从全局程序集缓存中导入 System.Messaging
命名空间代码:
[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
接下来,在 MessageQueue
实例上使用 SetPermission
时,您不需要再次提供队列名称 - 它已经知道它是谁了,可以这么说。
您做的需要提供的是您要向其授予消息队列访问权限的用户、安全组或计算机的用户名。
因此,如果您想授予自己的用户帐户对队列 "MyQueue" 的 FullControll 访问权限,并且您的用户名是 "david.wilson",则变为:
$Queue = [System.Messaging.MessageQueue]::Create(".\private$\MyQueue")
$Queue.SetPermissions("david.wilson",[System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Set)
希望此时 Server 2008 实例已经或正在停用,但无论如何您也可以使用 Powershell 5 创建队列。
function CreateQueue(
[string] $queueName,
[string] $queueType = 'Private'
) {
New-MsmqQueue -Name $queueName -QueueType $queueType
}
CreateQueue -queueName 'PrivateTestQueue'
https://docs.microsoft.com/en-us/powershell/module/msmq/new-msmqqueue?view=win10-ps