Friday, 13 November 2009

Delete all MSMQ queues at some PC with PowerShell

We have a small issue at one of our production servers. Once something generated ~10K queues with Guid-like names. Since then they were not deleted as there was a problem: standard MSMQ manager doesn't allow deleting several queues at once.

What to do? Powershell, of course! :) Here goes the script:


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

[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("someserver") | % {".\" + $_.QueueName} | % {[System.Messaging.MessageQueue]::Delete($_); }


You may also filter them by name if you do not need to remove them all as I need:


[System.Messaging.MessageQueue]::GetPrivateQueuesByMachine("someserver") | % {".\" + $_.QueueName} | ? {$_ -match "SOME_REGEX_FILTER"} | % {[System.Messaging.MessageQueue]::Delete($_); }

And SOME_REGEX_FILTER is... Yeap, some regex filter :) The -match operator allow us using regular expressions in the Where-Object (alias "?") clause.

Have fun with PowerShell!

5 comments:

myotherme said...

How do you run this?

Kiquenet said...

How can I purgue queues in remote server?

Leslie Lim said...

Thanks to the writer of this article. I appreciate your effort in making this informational blogs. I know it's not easy to do this but you have done a really great job. Congrats. I'm pretty sure your readers enjoying it a lots.


Rica
www.imarksweb.org

Silvia Jacinto said...

Thanks for sharing such a wonderful article, I hope you could inspire more people. Visit my site too.

n8fan.net

www.n8fan.net

sarah lee said...

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.


edupdf.org