Skip to content

Instantly share code, notes, and snippets.

@rdpetrusek
Created October 8, 2018 13:55
Show Gist options
  • Select an option

  • Save rdpetrusek/ecc4993f11b97923e215b291b4870809 to your computer and use it in GitHub Desktop.

Select an option

Save rdpetrusek/ecc4993f11b97923e215b291b4870809 to your computer and use it in GitHub Desktop.
Send MSMQ Message
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
function WriteMessageToMSMQTrans($queueName, $message, $label)
{
$queue = new-object System.Messaging.MessageQueue $queueName
$utf8 = new-object System.Text.UTF8Encoding
$tran = new-object System.Messaging.MessageQueueTransaction
$tran.Begin()
$msgBytes = $utf8.GetBytes($message)
$msgStream = new-object System.IO.MemoryStream
$msgStream.Write($msgBytes, 0, $msgBytes.Length)
$msg = new-object System.Messaging.Message
$msg.BodyStream = $msgStream
if ($label -ne $null)
{
$msg.Label = $label
}
$queue.Send($msg, $tran)
$tran.Commit()
Write-Host "Message written: " $message
}
WriteMessageToMSMQTrans "FormatName:DIRECT=OS:MACHINE_NAME\private$\QUEUE_NAME" $message "LABEL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment