I need to test some message queue processing functions and for that I need to push a message with contents of a json file. I’m currently forced to do it without using any 3rd party applications and as such, through powershell.
I’ve tried doing it this way.
Get-MsmqQueue -Name "queue" | Send-MsmqQueue -Body (Get-Content -Raw -Path "path_to_json") -Transactional
But I’m getting this as a result.
Send-MsmqQueue : There was an error generating the XML document.
At line:1 char:36
+ ..." | Send-MsmqQueue -Body (Get-Content -Raw -Path ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Send-MsmqQueue], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Msmq.PowerShell.Commands.SendMSMQQueueCommand
Operation works correctly if I write a simple string for -Body instead of the result of Get-Content), but pasting contents of file this way is not really feasible.
1
I have had success in the past sending a message to MSMQ using a memory stream. Below is a Function I use to accomplish this:
The parameters:
$queueName
= The queue name as it is displayed in theGet-MsmqQueue
command$message
= The body of the message as a string
Function:
Function SendMSMQmessage
{
Param(
$queueName,
$message
)
$queue = New-Object System.Messaging.MessageQueue $queueName
$utf8 = New-Object System.Text.UTF8Encoding
$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
$queue.Send($msg)
Write-Host "ORIGINAL MESSAGE: " -ForegroundColor Yellow
$message
}
Using the Function (example):
SendMSMQmessage -queueName "ServerQueueName" -message "I am a test"