在远程计算机上创建私人消息队列

 烟灰TT 发布于 2023-02-13 12:08

我已经读过,在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

结果是一样的.任何可能性?

1 个回答
  • 是的,似乎不支持通过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
    }
    

    2023-02-13 12:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有