Powershell MSMQ

I had to write a function to send MSMQ messages using powershell.  Here is the code that builds on an earlier post sending Unicode text messages with MSMQ

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

param($queueName,$messageLabel,$messageBody)
 
$messageBody = $messageBody.replace("\<\?.+\>\r\n", "").Trim()

if($messageBody.length -gt 0)
{
 $queue = new-object System.Messaging.MessageQueue($queueName)
 $msg = new-object System.Messaging.Message
 $msg.Label = $messageLabel
 $enc = new-object System.Text.UnicodeEncoding($false, $false)
 $writer = new-object System.IO.StreamWriter($msg.BodyStream, $enc)
 $writer.Write($messageBody)
 $writer.Flush()
 $msg.BodyType = 8
 $queue.Send($msg, [System.Messaging.MessageQueueTransactionType]::Single)
 write-host $messageBody

 }
}
Print | posted on Tuesday, March 18, 2014 9:12 AM