我已经读过,在C#中,我们无法在远程计算机上创建专用队列: 无法在远程服务器上创建专用消息队列
我的问题是:在PowerShell脚本中,我们可以这样做吗?这是我的示例脚本:
echo "Loading System.Messaging..." [Reflection.Assembly]::LoadWithPartialName( "System.Messaging" ) $msmq = [System.Messaging.MessageQueue] echo "Create the queue" $qName = "remoteserver\private$\testqueue" if($msmq::Exists($qName)) { echo ($qName + " already exists ") } else { echo ($qName + " doesn't exists and now to create ......") $q = $msmq::Create( $qName, $TRUE ) echo "Private queues has been created" }
它说"无效的队列路径名称".我也尝试过一些格式:FormatName:DIRECT=OS:remoteserver\private$\testqueue
结果是一样的.任何可能性?
是的,似乎不支持通过System.Messaging API创建远程队列,但所有都不会丢失!使用powershell远程处理(如@abatishchev建议的那样)来创建本地队列,它可以正常工作.
我将您的脚本保存到稍微修改的文件(create-queue.ps1)以进行设置$queueName = '.\private$\testqueue'
.然后我曾经invoke-command
在远程服务器上运行脚本:
Invoke-Command -ComputerName remoteserver -FilePath .\create-queue.ps1
这假设您已在目标服务器上启用了PowerShell远程处理.您应该在服务器完成后收到输出,以便您能够诊断出任何错误.
如果你必须做很多事情,你当然可以把它全部包装在一个很好的powershell函数中:
function Create-MessageQueue { param([string]$QueueName,[string]$ComputerName = ".") $script = { param($qName) [Reflection.Assembly]::LoadWithPartialName('System.Messaging') | out-debug $msmq = [System.Messaging.MessageQueue] $queuePath = ".\private`$\$qName" if($msmq::Exists($queuePath)) { echo "$queuePath already exists " } else { echo "'$queuePath' doesn't exists and now to create ......" $msmq::Create($queuePath,$true) echo "Private queue '$queuePath' has been created" } } Invoke-Command -ComputerName $ComputerName -ScriptBlock $script -ArgumentList $QueueName }