Friday, March 18, 2011

ReturnToSourceQueue PowerShell Script

The ReturnToSourceQueue.exe tool that comes with NSB presented a challenge to us when we tried to deploy it on all our app servers.  We needed to schedule an on-demand job to run the tool by our Operations group.  Since the agent we use goes on each machine and is not cluster aware, this presented an issue.  Our resolution was to replicate the same functionality in PS with a bit of a twist.  We added the ability to hit a remote queue.  The script was done by one of our developers Brandon Moriarty and I've posted it to GitHub for everyone to enjoy!  Big thanks to Brandon for whipping this one up.  Don't forget I have a few other PS scripts in there to do some quick and dirty things with MSMQ.


  1. Cool stuff!

    What about transactions, is there a risk that you might loose a message if the script terminates just after the read?

    Does powershell support TransactionScopes?

  2. Unfortunately PS doesn't support TransactionScope(I tried). What we ended up doing is using regular MessageQueueTransaction. See the updated script.

  3. If you want the replay the error Qs from remote server you can use PowerShell to remotely run ReturnToSourceQueue in the messaging server. Here is a link to a script which does that: