使用 PowerShell 管理服务总线资源Use PowerShell to manage Service Bus resources
Microsoft Azure PowerShell 是一个脚本编写环境,可用于控制和自动执行 Azure 服务的部署和管理。Microsoft Azure PowerShell is a scripting environment that you can use to control and automate the deployment and management of Azure services. 本文介绍如何通过本地 Azure PowerShell 控制台或脚本,使用服务总线 Resource Manager PowerShell 模块来预配和管理服务总线实体(命名空间、队列、主题和订阅)。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.
还可以使用 Azure Resource Manager 模板管理服务总线实体。You can also manage Service Bus entities using Azure Resource Manager templates. 有关详细信息,请参阅使用 Azure Resource Manager 模板创建服务总线资源一文。For more information, see the article Create Service Bus resources using Azure Resource Manager templates.
备注
本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介。To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShell。For Az module installation instructions, see Install Azure PowerShell.
先决条件Prerequisites
在开始之前,需要符合以下先决条件:Before you begin, you'll need the following prerequisites:
- Azure 订阅。An Azure subscription. 若要详细了解如何获取订阅,请参阅购买选项、会员套餐或试用帐户。For more information about obtaining a subscription, see purchase options, member offers, or trial account.
- 配备 Azure PowerShell 的计算机。A computer with Azure PowerShell. 有关说明,请参阅 Azure PowerShell cmdlet 入门。For instructions, see Get started with Azure PowerShell cmdlets.
- 大致了解 PowerShell 脚本、NuGet 包和 .NET Framework。A general understanding of PowerShell scripts, NuGet packages, and the .NET Framework.
入门Get started
第一步是使用 PowerShell 登录 Azure 帐户和 Azure 订阅。The first step is to use PowerShell to log in to your Azure account and Azure subscription. 按照 Azure PowerShell cmdlet 入门中的说明登录 Azure 帐户,检索并访问 Azure 订阅中的资源。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.
设置 Service Bus 命名空间Provision a Service Bus namespace
使用服务总线命名空间时,可以使用 Get-AzServiceBusNamespace、New-AzServiceBusNamespace、Remove-AzServiceBusNamespace 和 Set-AzServiceBusNamespace cmdlet。When working with Service Bus namespaces, you can use the Get-AzServiceBusNamespace, New-AzServiceBusNamespace, Remove-AzServiceBusNamespace, and Set-AzServiceBusNamespace cmdlets.
本示例在脚本中创建几个本地变量:$Namespace
和 $Location
。This example creates a few local variables in the script; $Namespace
and $Location
.
$Namespace
是要使用的服务总线命名空间的名称。$Namespace
is the name of the Service Bus namespace with which we want to work.$Location
标识我们要在其中预配命名空间的数据中心。$Location
identifies the data center in which we provision the namespace.$CurrentNamespace
存储我们检索(或创建)的引用命名空间。$CurrentNamespace
stores the reference namespace that we retrieve (or create).
在实际脚本中,$Namespace
和 $Location
可作为参数传递。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
下面的示例演示如何使用 New-AzServiceBusAuthorizationRule、Get-AzServiceBusAuthorizationRule、Set-AzServiceBusAuthorizationRule 和 Remove-AzServiceBusAuthorizationRule cmdlet 管理命名空间授权规则。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 -EnablePartitioning $True
$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
执行上一部分中的脚本后,可以使用 Set-AzServiceBusQueue cmdlet 更新队列的属性,如以下示例所示: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
设置其他 Service Bus 实体Provisioning other Service Bus entities
可以使用服务总线 PowerShell 模块预配其他实体,例如主题和订阅。You can use the Service Bus PowerShell module to provision other entities, such as topics and subscriptions. 这些 cmdlet 在语法上与上一部分所示的队列创建 cmdlet 类似。These cmdlets are syntactically similar to the queue creation cmdlets demonstrated in the previous section.
后续步骤Next steps
- 有关服务总线 Resource Manager PowerShell 模块的完整文档,请参阅此处。See the complete Service Bus Resource Manager PowerShell module documentation here. 此页列出所有可用的 cmdlet。This page lists all available cmdlets.
- 有关使用 Azure Resource Manager 模板的信息,请参阅使用 Azure Resource Manager 模板创建服务总线资源一文。For information about using Azure Resource Manager templates, see the article Create Service Bus resources using Azure Resource Manager templates.
- 有关服务总线 .NET 管理库的信息。Information about Service Bus .NET management libraries.
这些博客文章介绍管理服务总线实体的一些备选方法:There are some alternate ways to manage Service Bus entities, as described in these blog posts:
- How to create Service Bus queues, topics and subscriptions using a PowerShell script(如何使用 PowerShell 脚本创建服务总线队列、主题和订阅)How to create Service Bus queues, topics and subscriptions using a PowerShell script
- 如何使用 PowerShell 脚本创建 Service Bus 命名空间和事件中心How to create a Service Bus Namespace and an Event Hub using a PowerShell script
- 服务总线 PowerShell 脚本Service Bus PowerShell Scripts