PowerShell モジュールで Service Bus リソースを管理する

Microsoft Azure PowerShell は、Azure サービスのデプロイメントと管理を制御し自動化するために使用できるスクリプティング環境です。 この記事では、Service Bus Resource Manager の PowerShell モジュール を使用して、Service Bus エンティティ (名前空間、キュー、トピック、およびサブスクリプション) を、ローカルの Azure PowerShell コンソールまたはスクリプトでプロビジョニングおよび管理する方法について説明します。

Azure Resource Manager テンプレートでも Service Bus エンティティを管理できます。 詳細については、「Azure Resource Manager テンプレートを使用して Service Bus リソースを作成する」の記事を参照してください。

Note

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

前提条件

開始する前に、次の前提条件を満たす必要があります。

はじめに

最初の手順では、PowerShell を使用して、Azure アカウントと Azure サブスクリプションにログインします。 「Get started with Azure PowerShell cmdlets (Azure PowerShell コマンドレットの概要)」の手順に従って、Azure アカウントにログインし、Azure サブスクリプションのリソースを取得し、アクセスします。

Service Bus 名前空間のプロビジョニング

Service Bus 名前空間を操作する場合は、Get-AzServiceBusNamespaceNew-AzServiceBusNamespaceRemove-AzServiceBusNamespace、および Set-AzServiceBusNamespace コマンドレットを使用できます。

この例では、スクリプトでローカル変数 $Namespace$Location を作成します。

  • $Namespace は操作する Service Bus 名前空間の名前です。
  • $Location は名前空間をプロビジョニングするデータ センターを識別します。
  • $CurrentNamespace では取得 (または作成) する参照名前空間を保存します。

実際のスクリプトでは $Namespace$Location はパラメーターとして渡すことができます。

スクリプトのこの部分では、次を行います。

  1. 指定された名前の Service Bus 名前空間の取得しようとします。

  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-AzServiceBusAuthorizationRule、および Remove-AzServiceBusAuthorizationRule コマンドレットを使用して名前空間の承認規則を管理する方法を示しています。

# 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 コマンドレットを使用してキューのプロパティを更新できます。

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

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

その他の Service Bus エンティティのプロビジョニング

Service Bus PowerShell モジュール を使用してトピックやサブスクリプションなどの他のエンティティをプロビジョニングできます。 これらのコマンドレットは、前のセクションで説明したキュー作成コマンドレットに構文的に似ています。

次のステップ

これ以外の Service Bus エンティティを管理する方法については、次のブログ投稿で説明されています。