Azure Powershell : Loop through each service bus connections and queue to get ActiveMessageCount and deadLetterMessageCount
Following powershell script can be used to loop through each service bus connections and queue to get Activate Message count and Dead Letter message count.
Select-AzSubscription -Subscription "SubscriptionName"
# Fetch all SB namespaces in subscription
Write-Host "Getting SB Namespaces..."
$sbNameSpaces = Get-AzServiceBusNamespace
[System.Collections.ArrayList]$sbConnectionStrings = @()
Write-Host "Getting Namespace connection strings, please wait..."
foreach ($sbNameSpace in $sbNameSpaces) {
$sbResult = Get-AzServiceBusKey -ResourceGroupName $sbNameSpace.ResourceGroupName -Namespace $sbNameSpace.Name -Name RootManageSharedAccessKey
[void]$sbConnectionStrings.Add($sbResult)
}
# Loop all service bus connections
foreach ($sbConnectionString in $sbConnectionStrings) {
# Connect to the Mangement method
$managementClient = [Microsoft.Azure.ServiceBus.Management.ManagementClient]::new($sbConnectionString.PrimaryConnectionString)
#$test = [Microsoft.Azure.ServiceBus.Management.ManagementClient]::new($hello)
# Get queue name and use Regex to get namespace name
$sbQueues = $managementClient.GetQueuesAsync()
$NameSpace = [regex]::match($sbConnectionString.PrimaryConnectionString,'sb\:.*?\.').Value
# Loop all queues and parse for dead-letters
foreach ($sbQueue in $sbQueues.Result) {
$QueueName = $sbQueue.Path
$QueueRunTime = $managementClient.GetQueueRuntimeInfoAsync($QueueName)
$ActiveMessageCount = $QueueRunTime.Result.MessageCountDetails.activeMessageCount
$deadLetterMessageCount= $QueueRunTime.Result.MessageCountDetails.deadLetterMessageCount
}
}
Comments
Post a Comment