使用 PowerShell 管理服务总线资源

Microsoft Azure PowerShell 是一个脚本编写环境,可用于控制和自动执行 Azure 服务的部署和管理。 本文介绍如何通过本地 Azure PowerShell 控制台或脚本,使用服务总线 Resource Manager PowerShell 模块来预配和管理服务总线实体(命名空间、队列、主题和订阅)。

还可以使用 Azure Resource Manager 模板管理服务总线实体。 有关详细信息,请参阅使用 Azure Resource Manager 模板创建服务总线资源一文。

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

先决条件

在开始之前,需要符合以下先决条件:

入门

第一步是使用 PowerShell 登录 Azure 帐户和 Azure 订阅。 按照 Azure PowerShell cmdlet 入门中的说明登录 Azure 帐户,检索并访问 Azure 订阅中的资源。

设置 Service Bus 命名空间

使用服务总线命名空间时,可以使用 Get-AzServiceBusNamespaceNew-AzServiceBusNamespaceRemove-AzServiceBusNamespaceSet-AzServiceBusNamespace cmdlet。

本示例在脚本中创建几个本地变量:$Namespace$Location

  • $Namespace 是要使用的服务总线命名空间的名称。
  • $Location 标识我们要在其中预配命名空间的数据中心。
  • $CurrentNamespace 存储我们检索(或创建)的引用命名空间。

在实际脚本中,$Namespace$Location 可作为参数传递。

此部分脚本执行以下任务:

  1. 尝试使用指定名称检索服务总线命名空间。

  2. 如果找到该命名空间,则报告它找到的内容。

  3. 如果找不到该命名空间,则会创建该命名空间,并检索新创建的命名空间。

    # 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."
    
    }
    

创建命名空间授权规则

下面的示例演示如何使用 New-AzServiceBusAuthorizationRuleGet-AzServiceBusAuthorizationRuleSet-AzServiceBusAuthorizationRuleRemove-AzServiceBusAuthorizationRule cmdlet 管理命名空间授权规则。

# 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
}

创建队列

若要创建队列或主题,请使用上一部分中的脚本执行命名空间检查。 然后,创建队列:

# 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."
}

修改队列属性

执行上一部分中的脚本后,可以使用 Set-AzServiceBusQueue cmdlet 更新队列的属性,如以下示例所示:

$CurrentQ.DeadLetteringOnMessageExpiration = $True
$CurrentQ.MaxDeliveryCount = 7
$CurrentQ.MaxSizeInMegabytes = 2048
$CurrentQ.EnableExpress = $True

Set-AzServiceBusQueue -ResourceGroup $ResGrpName -NamespaceName $Namespace -QueueName $QueueName -QueueObj $CurrentQ

设置其他 Service Bus 实体

可以使用服务总线 PowerShell 模块预配其他实体,例如主题和订阅。 这些 cmdlet 在语法上与上一部分所示的队列创建 cmdlet 类似。

后续步骤

这些博客文章介绍管理服务总线实体的一些备选方法: