Saturday, November 27, 2010

Retrieving message counts of a remote MSMQ queue using PowerShell

My current client does work for the retail industry. This weekend is the beginning of the holiday shopping season, a very important period to the retail industry.

This is also the first time that I’ve seen the planning and operations that the Retail IT groups go through. I’m really impressed by the amount of planning and support that is in place to make sure that IT-related issues don’t interfere with holiday shoppers. There are bridge calls that begin at 2am, around the clock monitoring of servers, hourly reports, call-ins every 30 minutes and lots of people are either active or on-call.

My part in this was to spend three hours monitoring 13 remote servers. To make life a little easier, we used a PowerShell script similar to this to retrieve the queue lengths of some remote MSMQ queues.

   1: $queuesToCheck = 'Q1' , 'Q2'
   2: $servers = 'myserver1', 'myserver2'
   3:  
   4: $queues = @()
   5: $servers | ForEach-Object {
   6:     $ServerName = $_
   7:     
   8:     $machineQueuesToCheck = $queuesToCheck | ForEach-Object { "$ServerName\$_".ToLower() }
   9:     $queues += gwmi -class Win32_PerfRawData_MSMQ_MSMQQueue -computerName $ServerName  | Where-Object {
  10:         $machineQueuesToCheck -contains $_.Name.Trim() -and $_.MessagesInQueue -gt 0
  11:     } 
  12: }
  13:  
  14: $queues | Format-Table Name, MessagesInQueue