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.
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.
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.
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