0

I was wondering if it was possible to delete queues remotely via PowerShell? I have the following script:

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);        
    }
}

This works fine, if I was running it on the machine whose queues I want to delete however when I run this remotely I get the error:

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

Any ideas if this can be done?

EDIT

Oddly, I have figured I can remote onto the machine via PowerShell and execute a script block. However, I don't understand the difference between doing:

THIS:

$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) };

AND THIS:

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

The value of $endpoint is the same however, for some odd reason it doesn't like the variable approach though both values are identical. I tested this by setting $endpoint then calling delete. I get the error:

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

What I'm trying to say is if I hard code the value as part of the argument it works but assign it to a variable then invoke the method I get an error

Dr Schizo
  • 4,045
  • 7
  • 38
  • 77
  • Might want to check [this](http://stackoverflow.com/questions/17174115/the-specified-format-name-does-not-support-the-requested-operation-for-example) out. The error message may not be lying about the need for a different name format. – Bacon Bits Aug 26 '15 at 16:09

3 Answers3

2

For historic purposes if anyone else is experiencing this issue or is wondering how to delete queues remotely then please see below.

  1. How do I delete private queues on a remote computer? It is possible to delete queues remotely. This can be achieved using the command Enable-PSRemoting -Force. Without this, you encounter the issue @JohnBreakWell indicated (see his link to MSDN).

  2. The scope of variables when using Invoke-Command? The problem I found was the variables I declared were simply out of scope (script block was unable to see it). To rectify this, I simply did the following:

The important bit being the argument list and the use of param.

 $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

    }
Dr Schizo
  • 4,045
  • 7
  • 38
  • 77
0

You cannot delete a remote private queue. You need to perform the operation locally to the queue.

From MQDeleteQueue:

Remarks
(2nd paragraph)
"Private queues registered on a remote computer ... cannot be deleted."

John Breakwell
  • 4,667
  • 20
  • 25
  • I am aware of the documentation :) which is why I opted to use PowerShell remote commands. Please refer to the updated post which I hope will shed some light on the matter. I able to delete private queues if I use Enable-PSRemoting – Dr Schizo Aug 28 '15 at 07:26
  • Apologies - I missed the actual question about why the two script blocks differ. Remoting works as you're then dealing with a local queue as far as the delete command is concerned. – John Breakwell Sep 01 '15 at 12:14
0

As Dr. Schizo mentioned, you'll need to execute

Enable-PSRemoting -Force

on the remote machine, but then, assuming you're using Server 2012 r2, it's as simple as:

Invoke-Command -ComputerName COMPUTERNAME { Get-MsmqQueue -Name QUEUENAME | Remove-MsmqQueue }
Joel
  • 86
  • 4