Use PowerShell to manage Service Bus resources
Microsoft Azure PowerShell is a scripting environment that you can use to control and automate the deployment and management of Azure services. This article describes how to use the Service Bus Resource Manager PowerShell module to provision and manage Service Bus entities (namespaces, queues, topics, and subscriptions) using a local Azure PowerShell console or script.
You can also manage Service Bus entities using Azure Resource Manager templates. For more information, see the article Create Service Bus resources using Azure Resource Manager templates.
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
Prerequisites
Before you begin, you'll need the following prerequisites:
- An Azure subscription. For more information about obtaining a subscription, see purchase options, member offers, or trial subscription.
- A computer with Azure PowerShell. For instructions, see Get started with Azure PowerShell cmdlets.
- A general understanding of PowerShell scripts, NuGet packages, and the .NET Framework.
Get started
The first step is to use PowerShell to log in to your Azure account and Azure subscription. Follow the instructions in Get started with Azure PowerShell cmdlets to log in to your Azure account, and retrieve and access the resources in your Azure subscription.
Provision a Service Bus namespace
When working with Service Bus namespaces, you can use the Get-AzServiceBusNamespace, New-AzServiceBusNamespace, Remove-AzServiceBusNamespace, and Set-AzServiceBusNamespace cmdlets.
This example creates a few local variables in the script; $Namespace
and $Location
.
$Namespace
is the name of the Service Bus namespace with which we want to work.$Location
identifies the data center in which we provision the namespace.$CurrentNamespace
stores the reference namespace that we retrieve (or create).
In an actual script, $Namespace
and $Location
can be passed as parameters.
This part of the script does the following:
Attempts to retrieve a Service Bus namespace with the specified name.
If the namespace is found, it reports what was found.
If the namespace is not found, it creates the namespace and then retrieves the newly created namespace.
# Query to see if the namespace currently exists $CurrentNamespace = Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace # Check if the namespace already exists or needs to be created if ($CurrentNamespace) { Write-Host "The namespace $Namespace already exists in the $Location region:" # Report what was found Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace } else { Write-Host "The $Namespace namespace does not exist." Write-Host "Creating the $Namespace namespace in the $Location region..." New-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace -Location $Location $CurrentNamespace = Get-AzServiceBusNamespace -ResourceGroup $ResGrpName -NamespaceName $Namespace Write-Host "The $Namespace namespace in Resource Group $ResGrpName in the $Location region has been successfully created." }
Create a namespace authorization rule
The following example shows how to manage namespace authorization rules using the New-AzServiceBusAuthorizationRule, Get-AzServiceBusAuthorizationRule, Set-AzServiceBusAuthorizationRule, and Remove-AzServiceBusAuthorizationRule cmdlets.
# Query to see if rule exists
$CurrentRule = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule
# Check if the rule already exists or needs to be created
if ($CurrentRule)
{
Write-Host "The $AuthRule rule already exists for the namespace $Namespace."
}
else
{
Write-Host "The $AuthRule rule does not exist."
Write-Host "Creating the $AuthRule rule for the $Namespace namespace..."
New-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule -Rights @("Listen","Send")
$CurrentRule = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule
Write-Host "The $AuthRule rule for the $Namespace namespace has been successfully created."
Write-Host "Setting rights on the namespace"
$authRuleObj = Get-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthorizationRuleName $AuthRule
Write-Host "Remove Send rights"
$authRuleObj.Rights.Remove("Send")
Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj
Write-Host "Add Send and Manage rights to the namespace"
$authRuleObj.Rights.Add("Send")
Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj
$authRuleObj.Rights.Add("Manage")
Set-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -AuthRuleObj $authRuleObj
Write-Host "Show value of primary key"
$CurrentKey = Get-AzServiceBusKey -ResourceGroup $ResGrpName -NamespaceName $Namespace -Name $AuthRule
Write-Host "Remove this authorization rule"
Remove-AzServiceBusAuthorizationRule -ResourceGroup $ResGrpName -NamespaceName $Namespace -Name $AuthRule
}
Create a queue
To create a queue or topic, perform a namespace check using the script in the previous section. Then, create the queue:
# Check if queue already exists
$CurrentQ = Get-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName
if($CurrentQ)
{
Write-Host "The queue $QueueName already exists in the $Location region:"
}
else
{
Write-Host "The $QueueName queue does not exist."
Write-Host "Creating the $QueueName queue in the $Location region..."
New-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName
$CurrentQ = Get-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName
Write-Host "The $QueueName queue in Resource Group $ResGrpName in the $Location region has been successfully created."
}
Modify queue properties
After executing the script in the preceding section, you can use the Set-AzServiceBusQueue cmdlet to update the properties of a queue, as in the following example:
$CurrentQ.DeadLetteringOnMessageExpiration = $True
$CurrentQ.MaxDeliveryCount = 7
$CurrentQ.MaxSizeInMegabytes = 2048
$CurrentQ.EnableExpress = $True
Set-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName -QueueObj $CurrentQ
Provisioning other Service Bus entities
You can use the Service Bus PowerShell module to provision other entities, such as topics and subscriptions. These cmdlets are syntactically similar to the queue creation cmdlets demonstrated in the previous section.
Next steps
- See the complete Service Bus Resource Manager PowerShell module documentation here. This page lists all available cmdlets.
- For information about using Azure Resource Manager templates, see the article Create Service Bus resources using Azure Resource Manager templates.
- Information about Service Bus .NET management libraries.
There are some alternate ways to manage Service Bus entities, as described in these blog posts: