Read-HTMsmqMessage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Read-HTMsmqMessage {
    [CmdletBinding()]
    param (
        # Parameter help description
        [Parameter(Mandatory=$true)]
        [String]
        $QueueNamePattern
    )
    
    process {

        if ( [System.String]::IsNullOrEmpty($QueueNamePattern) )
        {
            write-host "You must provide a queue name"
            return
        }else{

            $tt = Get-MsmqQueue | Where-Object { $_.QueueName -like  $QueueNamePattern } | Select-Object -First 1
            $qname = ".\$($tt.QueueName)"
        }

        [Reflection.Assembly]::LoadWithPartialName("System.Messaging") | out-null

        $q = new-object System.Messaging.MessageQueue($qname)
        
        $msgs = $q.GetAllMessages()
        
        foreach ( $msg in $msgs )
        {
            $msg.BodyStream.Position = 0
            $sr = new-object System.IO.StreamReader( $msg.BodyStream )
            $m = $sr.ReadToEnd()
            Clear-Host
            Write-Host $m
            $m | Out-File -FilePath ".\lastmessage.json" -Encoding utf8
            $input = read-host -prompt "Enter n for the next message, q to quit"
            
            if ( $input -eq "q" )
            {
                Write-Host "Exiting.."
                return
            }
        }
    }
    
}