在Windows Server 2008 R2上的PowerShell中创建专用队列

问题描述 投票:0回答:2

我用谷歌搜索了如何执行此操作,并找到了以下代码:

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不兼容吗?我输入的公司价值超过了最初的价值。

windows powershell server windows-server-2008-r2
2个回答
1
投票

您的代码似乎有两个问题。

首先,您需要告诉PowerShell从全局程序集缓存中导入System.Messaging名称空间代码:

[System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null

接下来,在SetPermission实例上使用MessageQueue时,无需再次提供队列名称-可以这么说,它已经知道它是谁。

您需要提供的是您要向其授予对消息队列的访问权限的用户,安全组或计算机的用户名。 因此,如果您要授予您自己的用户帐户对队列“ 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

0
投票
希望Server 2008实例已经或正在退役,但是无论如何您都可以使用Powershell 5创建队列。
© www.soinside.com 2019 - 2024. All rights reserved.