【问题标题】:Powershell delete MSMQ remotelyPowershell远程删除MSMQ
【发布时间】:2016-07-26 23:45:07
【问题描述】:

我想知道是否可以通过 PowerShell 远程删除队列?我有以下脚本:

cls

[Reflection.Assembly]::LoadWithPartialName("System.Messaging")

$computers = @("comp1","comp2","comp3");

foreach($computer in $computers) {

    $messageQueues = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine($computer);

    foreach ($queue in $messageQueues) {
        $endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);

        Write-Host $endpoint
        [System.Messaging.MessageQueue]::Delete($endpoint);        
    }
}

如果我在要删除其队列的机器上运行它,这很好用,但是当我远程运行它时,我得到了错误:

The specified format name does not support the requested operation. For example, a direct queue format name cannot be deleted.

如果可以做到这一点,有什么想法吗?

编辑

奇怪的是,我发现我可以通过 PowerShell 远程访问机器并执行脚本块。但是,我不明白这样做之间的区别:

这个:

$endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);
Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete($endpoint) };

还有这个:

Invoke-Command -ComputerName $computer -ScriptBlock { [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); [System.Messaging.MessageQueue]::Delete("FormatName:DIRECT=OS:MY_SERVER\some.endpoint") };

$endpoint 的值是相同的,但是由于某些奇怪的原因,它不喜欢变量方法,尽管两个值是相同的。我通过设置$endpoint 然后调用delete 对此进行了测试。我得到错误:

Exception calling "Delete" with "1" argument(s): "Invalid value  for parameter path."

我想说的是,如果我将值硬编码为它工作的参数的一部分,但将它分配给一个变量然后调用我得到一个错误的方法

【问题讨论】:

  • 可能想签出this。错误消息可能不是说需要不同的名称格式。

标签: powershell msmq


【解决方案1】:

出于历史目的,如果其他人遇到此问题或想知道如何远程删除队列,请参阅下文。

  1. 如何删除远程计算机上的专用队列?可以远程删除队列。这可以使用命令Enable-PSRemoting -Force 来实现。如果没有这个,您会遇到@JohnBreakWell 指出的问题(请参阅他的 MSDN 链接)。

  2. 使用 Invoke-Command 时变量的作用域? 我发现的问题是我声明的变量超出了作用域(脚本块无法看到它)。为了解决这个问题,我简单地做了以下操作:

重要的一点是argument listparam 的使用。

 $computers = @("comp1","comp2");

    foreach($computer in $computers) {
        [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); 

        $messageQueues = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine($computer);

        foreach ($queue in $messageQueues) {
            $endpoint = [string]::Format("FormatName:DIRECT=OS:{0}\{1}", $computer, $queue.QueueName);

            Enable-PSRemoting -Force
            Invoke-Command -ComputerName $computer -ScriptBlock {
                param ($computer, $endpoint)
                [Reflection.Assembly]::LoadWithPartialName("System.Messaging"); 
                [System.Messaging.MessageQueue]::Delete($endpoint) 
            }

        } -ArgumentList $computer, $endpoint

    }

【讨论】:

  • 这太好了,我更同意你这样的名字,你一定是个相当大的小伙子
  • 是的,但我有点像巨石强森
【解决方案2】:

您无法删除远程专用队列。 您需要在队列本地执行操作。

来自MQDeleteQueue

备注
(第二段)
“在远程计算机上注册的私人队列......无法删除。”

【讨论】:

  • 我知道文档 :) 这就是我选择使用 PowerShell 远程命令的原因。请参阅更新后的帖子,我希望能对此事有所了解。如果我使用 Enable-PSRemoting,我可以删除私有队列
  • 道歉 - 我错过了关于为什么两个脚本块不同的实际问题。就删除命令而言,远程处理就像您处理本地队列一样工作。
【解决方案3】:

正如 Schizo 博士所说,您需要执行

Enable-PSRemoting -Force

在远程机器上,但是,假设您使用的是 Server 2012 r2,它很简单:

Invoke-Command -ComputerName COMPUTERNAME { Get-MsmqQueue -Name QUEUENAME | Remove-MsmqQueue }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 2016-05-29
    • 2012-08-12
    • 1970-01-01
    • 2016-06-25
    • 2012-12-03
    • 2016-08-10
    • 2010-10-02
    相关资源
    最近更新 更多