Windows 在远程计算机上创建专用消息队列
我已经读到,在C#中,我们不能在远程机器上创建私有队列: 我的问题是:在PowerShell脚本中,我们可以这样做吗?以下是我的示例脚本:Windows 在远程计算机上创建专用消息队列,windows,powershell,message-queue,powershell-remoting,Windows,Powershell,Message Queue,Powershell Remoting,我已经读到,在C#中,我们不能在远程机器上创建私有队列: 我的问题是:在PowerShell脚本中,我们可以这样做吗?以下是我的示例脚本: echo "Loading System.Messaging..." [Reflection.Assembly]::LoadWithPartialName( "System.Messaging" ) $msmq = [System.Messaging.MessageQueue] echo "Create the queue" $qName = "rem
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
结果是一样的。有什么可能性吗?我从文档中收集到的一些东西:
FormatName
前缀。«的文档说明,因此您声明队列名称的替代方法也无法工作$
在PowerShell中具有重要意义。它用于内联变量扩展。它在这里没有什么意义,因为它没有形成有效的变量名(或子表达式),因此仍然是逐字逐句的。但这里应该使用单引号字符串。如果有疑问,只需将字符串转储到管道上即可查看其值Workgroup mode Available
=====================================================================
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name No
这就意味着它不适用于远程计算机名我从文档中收集了一些信息:
FormatName
前缀。«的文档说明,因此您声明队列名称的替代方法也无法工作$
在PowerShell中具有重要意义。它用于内联变量扩展。它在这里没有什么意义,因为它没有形成有效的变量名(或子表达式),因此仍然是逐字逐句的。但这里应该使用单引号字符串。如果有疑问,只需将字符串转储到管道上即可查看其值Workgroup mode Available
=====================================================================
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name No
这就意味着它不适用于远程计算机名是,似乎是通过系统创建远程队列。不支持消息传递API,但不会全部丢失!使用powershell远程处理(正如@abatishchev所建议的)来创建本地队列,它可以正常工作 我将您的脚本保存到一个文件(create queue.ps1)中,该文件稍微修改为设置
$queueName='.\private$\testqueue'
。然后我使用invoke命令
在远程服务器上运行脚本:
Invoke-Command -ComputerName remoteserver -FilePath .\create-queue.ps1
这假定您已在目标服务器上启用powershell远程处理。您应该在服务器完成时接收到输出,以便能够诊断任何错误
当然,如果您需要经常使用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
}
是,似乎是通过系统创建远程队列。不支持消息传递API,但不会全部丢失!使用powershell远程处理(正如@abatishchev所建议的)来创建本地队列,它可以正常工作 我将您的脚本保存到一个文件(create queue.ps1)中,该文件稍微修改为设置
$queueName='.\private$\testqueue'
。然后我使用invoke命令
在远程服务器上运行脚本:
Invoke-Command -ComputerName remoteserver -FilePath .\create-queue.ps1
这假定您已在目标服务器上启用powershell远程处理。您应该在服务器完成时接收到输出,以便能够诊断任何错误
当然,如果您需要经常使用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
}
我想,您真正需要的是
输入PSSession
。然后创建一个您需要的“本地”队列。嗨,abatishchev,我尝试了您的解决方案。奇怪的是:执行脚本时出错,但我看不到远程服务器上的队列。我想您真正需要的是进入PSSession
。然后创建一个您需要的“本地”队列。嗨,abatishchev,我尝试了您的解决方案。奇怪的是:执行脚本时出错,但我看不到远程服务器上的队列。嗨,Joey,这是否意味着无法通过Powershell在远程服务器上创建私有消息队列?我不知道,也从未处理过这些问题。我只是从文档中指出了一些可能有用的东西。同样,这与PowerShell无关。您可以用C#编写完全相同的代码,但它不起作用。毕竟,您只调用框架对象的方法。嗨,Joey,这是否意味着无法通过Powershell在远程服务器上创建私有消息队列?我不知道,也从未使用过这些方法。我只是从文档中指出了一些可能有用的东西。同样,这与PowerShell无关。您可以用C#编写完全相同的代码,但它不起作用。毕竟你只调用框架对象的方法。嗨,迈克,太棒了!这真的有效吗?在运行Windows远程管理并根据@ChadLazette AFAIK启用Windows远程管理的计算机上尝试创建远程专用队列时,我仍然收到“无效队列路径名”错误。我在写这篇文章的时候测试过。嗨,迈克,太棒了!这真的有效吗?在运行Windows远程管理并根据@ChadLazette AFAIK启用Windows远程管理的计算机上尝试创建远程专用队列时,我仍然收到“无效队列路径名”错误。我在写这篇文章时测试过它。