none
Peeking at messages in MSMQ with PowerShell... RRS feed

  • Question

  • Hello,

    I am a n00b to PowerShell and I am trying to create some maintenance script to manage our MSMQs ... so go easy on me! :P  I am currently trying to create a filtered list of message IDs by providing a cutoff date.  The problem is when I try to grab the ArriveTime or SentTime from the message it is always null-valued expression (and coughs up an exception).  Oh, yeah!  The messages I'm trying to sort through are in the journal.

    I have written these functions based on various example (C# and PS) on the Net.  There could be some gross inefficiencies and a lot of overkill but I think you get the basic idea.

    function PeekWithoutTimeout([System.Messaging.MessageQueue] $q, [System.Messaging.Cursor] $cursor, [System.Messaging.PeekAction] $action) { [System.Messaging.Message] $Return = $null [TimeSpan] $ts = [TimeSpan] '00:00:01' try { $Return = $q.Peek($ts, $cursor, $action) } catch [System.Messaging.MessageQueueException] { $ErrorMessage = $_.Exception.Message if (!$ErrorMessage.ToLower().Contains("timeout")) { throw $ErrorMessage } } return $Return } function GetFilteredMessageList([System.Messaging.MessageQueue] $q, [System.DateTime] $cutoff) { [System.Collections.ArrayList] $list = New-Object -TypeName System.Collections.ArrayList [System.DateTime] $sentTime = New-Object -TypeName System.DateTime [System.Messaging.Cursor] $cursor = $q.CreateCursor() $peekActionCurrent = [System.Messaging.PeekAction]::Current $peekActionNext = [System.Messaging.PeekAction]::Next [System.Messaging.Message] $m = PeekWithoutTimeout $q $cursor $peekActionCurrent if ($m -ne $null) { $sentTime = [System.DateTime]::Parse($m.SentTime.ToString()) #exception here Write-Host $sentTime #debug statement if($sentTime -le $cutoff) { $list.Add($m.Id.ToString()) } while (($m = PeekWithoutTimeout $q $cursor $peekActionNext) -ne $null) { $sentTime = [System.DateTime]::Parse($m.SentTime.ToString()) if($sentTime -le $cutoff) { $list.Add($m.Id.ToString()) } } } return $list.ToArray() }

    $cutoff = [System.DateTime]::Parse("11/7/2013 11:58:30 AM")
    GetFilteredMessageList ".\private$\main\Journal$" $cutoff

    Is there a better way to do this?

    TIA!

    Friday, November 14, 2014 12:00 AM

Answers

  • Thanks,

    I found out with a bit more Googling I need to use the line:

         $q.MessageReadPropertyFilter.SetAll()

    to get all the properties I need.

    *sigh*

    • Marked as answer by 7fold Friday, November 14, 2014 7:57 PM
    Friday, November 14, 2014 7:57 PM

All replies

  • If you know MQ then you know ther are no efficiencies in peeking.  It is a utility function.

    If it works be set.  The code seems normal.  I see no big issues.

    C# examples are a good place to start but be adventurous, apply PowerShell to the task.  Processing messages is a good candidate for the pipeline.

    MQ is easy.  It only becomes an issue when you add work flows to it.


    ¯\_(ツ)_/¯

    Friday, November 14, 2014 3:10 AM
  • Thanks,

    I found out with a bit more Googling I need to use the line:

         $q.MessageReadPropertyFilter.SetAll()

    to get all the properties I need.

    *sigh*

    • Marked as answer by 7fold Friday, November 14, 2014 7:57 PM
    Friday, November 14, 2014 7:57 PM