Fastest method to get MSMQ message counts

Getting MSMQ message counts can be painfully slow if there are large numbers of messages in a given queue. This PowerShell script is the fastes known way I have found to get the message counts of a queue. The time to get the counts is linear regardless of the number of messages.


<#
.SYNOPSIS
Gets all queues' information from the specified computer

.DESCRIPTION
Gets all queues' information from the specified computer

Adapted from http://www.codeproject.com/Articles/346575/Message-Queue-Counting-Comparisions

.EXAMPLE
Get-MessageQueueInfo.ps1 -MachineName rg-p-blu2 | sort -Property name| ft -Property Name,MessagesInQueue,BytesInQueue -AutoSize
#>

param([string] $MachineName = $env:COMPUTERNAME)
$path = "\\{0}\root\CIMv2" -f $MachineName
$scope = new-object System.Management.ManagementScope ($path)
$scope.Connect()
$QueryString = "SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue"
$query = new-object System.Management.ObjectQuery ($queryString)
$searcher = new-object System.Management.ManagementObjectSearcher ($scope, $query)
$searcher.Get()



Advertisements