Write MSMQ Messages from PowerShell

This PowerShell script sends a message to an MSMQ. I did it this particular way because NServiceBus can consume messages sent like this.

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

$queueName = '.\Private$\your.queue.name';
$queue = new-object System.Messaging.MessageQueue $queueName;
$utf8 = new-object System.Text.UTF8Encoding;

$tran = new-object System.Messaging.MessageQueueTransaction;
$tran.Begin();

$msgContent = '



    ';
$msgBytes = $utf8.GetBytes($msgContent);

$msgStream = new-object System.IO.MemoryStream;
$msgStream.Write($msgBytes, 0, $msgBytes.Length);

$msg = new-object System.Messaging.Message;
$msg.BodyStream = $msgStream;
$queue.Send($msg, $tran);

$tran.Commit();

Comments !

links

social