Search code examples
powershellmsmq

Convert BodyStream in MSMQ to text/XML via PoSH?


What I'm trying to do: PEEK multiple MSMQ messages (not RECEIVE) using PowerShell, then return the contents of BodyStream in XML format (it appears to be in Decimal) so that I can shred the XML in the message and return information from within it.

We're trying to look and see what's still in the queue, what they have in common, what the most recent values in the XML are, etc. I've gone looking which is how I've gotten this far, but I haven't even figured how to peek multiple messages yet.

Here's basic code that will PEEK the first message in the "mybundles" private queue - ideally I'd like to do many more, maybe even the entirety (though again, without pulling them off the queue)

[Reflection.Assembly]::LoadWithPartialName("System.Messaging")
$queue = new-object System.Messaging.MessageQueue "FormatName:DIRECT=OS:servername\private$\mybundles"
$queue.Formatter.TargetTypes = ,[string]
$peekmsg = $queue.Peek()

[string]$sr = $peekmsg.BodyStream.ToArray()
$sr

When I do this, it returns a string of numbers, which is a decimal representation of the text I want. How do I change that string of numbers (or straight from BodyStream to text/XML?


Solution

  • Looks like you do this:

    $peekmsg = $queue.Peek()
    $sr= $peekmsg.BodyStream.ToArray() 
    $enc = [System.Text.Encoding]::ASCII
    $enc.GetString($sr)
    

    I think I'd just do a foreach loop, once I figure out how to PEEK multiple.