Absolute beginner bit off too much on his first step with PS and MSMQ..

Welcome Forums General PowerShell Q&A Absolute beginner bit off too much on his first step with PS and MSMQ..

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #122771

    Participant
    Points: 13
    Rank: Member

    Scenario:

    Once in while, a message enters a specific transactional message queue on an application server.

    If this message contains 'xxx', I have to delete it before it gets picked up and 'poisons' the treating process.

    So far, I've got this:

    [System.Reflection.Assembly]::LoadWithPartialName("System.Messaging") $queuePath = ".\private$\soapoutbound" $queue = New-Object System.Messaging.MessageQueue $queuePath foreach ($message in $queue.GetAllMessages()){ Write-Host (New-Object System.Text.UTF8Encoding).GetString($message.BodyStream.ToArray()) }

    But it seems that I cannot use the -contains clause on this resulting array (will always return false).

    Can anyone point me in the direction of a more example-based learning resource for this one?

    Many thanks!

     

  • #122784

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    Where are you trying to use contains, can you pass little more details .

    • #122792

      Participant
      Points: 13
      Rank: Member

      Apologies indeed.. 🙂

      I will then try to concatenate the bodystream of 'every' message in the queue into one big String (usually only a few messages per minute max),

      and use the -contain clause on that big String(UTF8Encoding). Still trying to find out how to do that.

      If that comes out True, then I go looking for a way to delete the first message in the queue (because that one will be the offending one).

      Anyway, that's my 'battle plan'.

  • #123111

    Participant
    Points: 305
    Helping Hand
    Rank: Contributor

    The -contains comparison operator validates if a term is in an array returning a boolean result:

    $colors = 'red','white','blue'
    
    $colors -contains 'red' #true
    $colors -contains 'darkred' #false, does not support wildcard search
    

    The -like comparison operator validates a wild card term is found in a string retuning a boolean result

    $searchTerm = '*xxx*'
    
    'Our programming selection contains xxx material' -like $searchTerm #true
    

You must be logged in to reply to this topic.