apr 3 blog

PowerShell and MSMQ – Move messages between queues

Use the script below to move MSMQ messages between queues, this script will move all messages in the source queue


# Load required dll
[Reflection.Assembly]::LoadWithPartialName(“System.Messaging”)

#Set Up Queues
$vQueueFromName = “.private$scriberetry”
$vQueueFromTo = “.private$scribein”

#$vQueueFromName = “.private$test_in"
#$vQueueFromTo = “.private$test_new"

$qFrom = new-object System.Messaging.MessageQueue $vQueueFromName
$qTo = new-object System.Messaging.MessageQueue $vQueueFromTo

Write-Host ("COUNT OF MESSAGES IN " + $vQueueFromName +" " + $qFrom.GetAllMessages().Length) + "MESSAGES";
$vTotalCountInMSMQ = $qFrom.GetAllMessages().Length

if ($vTotalCountInMSMQ -gt 0)
{
 $i = 1
 ## counter
 do
  {
   $qTo.send($qFrom.Receive())
   #Write-Host ("Messages left in " + $vQueueFromName +" " + $qFrom.GetAllMessages().Length);
   Write-Host $i
   $i++
  }
 until ($i -eq $vTotalCountInMSMQ +1)
}

Write-Host ("Message move is done");
Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+