1

How can I purge/delete message programmatically from MS Queue in remote server from a client machine using a script (Powershell) or command line tool (C#)?

My user (credentials) in client machine has FULL CONTROL in MS Queue in remote server.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
  • interesting reading: http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.delete(v=vs.110).aspx & http://stackoverflow.com/a/915127/520612 – CB. Jul 29 '14 at 09:39

2 Answers2

2

Try this:

Invoke-Command -ComputerName "mycomputer" -ScriptBlock {

        ## if public then $queuename = ".\YOUR_Q_NAME" 
        $queuename = ".\private$\YOUR_Q_NAME"  

        [Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
        $queue = New-Object -TypeName "System.Messaging.MessageQueue"
        $queue.Path = $queuename 
        $messagecount = $queue.GetAllMessages().Length
        $queue.Purge()
        Write-Host "$queuename has been purged of $messagecount messages."
    }

Note: Requires PS Remoting enabled in Remote Server.

Raf
  • 9,681
  • 1
  • 29
  • 41
1

Or if you're running Windows Server 2012 on the remote machine, you can use the MSMQ module functions:

Get-MessageQueue -Name QueueName | Clear-MessageQueue
Keith Hill
  • 194,368
  • 42
  • 353
  • 369