Använda PowerShell för att hantera Service Bus resurser

Microsoft Azure PowerShell är en skriptmiljö som du kan använda för att styra och automatisera distributionen och hanteringen av Azure-tjänster. Den här artikeln beskriver hur du använder PowerShell-modulen Service Bus Resource Manager för att etablera och hantera Service Bus-entiteter (namnrymder, köer, ämnen och prenumerationer) med hjälp av en lokal Azure PowerShell konsol eller ett skript.

Du kan också hantera Service Bus med hjälp Azure Resource Manager mallar. Mer information finns i artikeln Skapa Service Bus med hjälp av Azure Resource Manager mallar.

Anteckning

I den här artikeln används Azure Az PowerShell-modulen, som är den rekommenderade PowerShell-modulen för att interagera med Azure. För att komma igång med Az PowerShell kan du läsa artikeln om att installera Azure PowerShell. Information om hur du migrerar till Az PowerShell-modulen finns i artikeln om att migrera Azure PowerShell från AzureRM till Az.

Förutsättningar

Innan du börjar behöver du följande:

Kom igång

Det första steget är att använda PowerShell för att logga in på ditt Azure-konto och din Azure-prenumeration. Följ instruktionerna i Kom igång med Azure PowerShell-cmdlets för att logga in på ditt Azure-konto och hämta och komma åt resurserna i din Azure-prenumeration.

Etablera ett Service Bus namnområde

När du arbetar med Service Bus-namnområden kan du använda cmdletarna Get-AzServiceBusNamespace, New-AzServiceBusNamespace, Remove-AzServiceBusNamespaceoch Set-AzServiceBusNamespace.

Det här exemplet skapar några lokala variabler i skriptet. $Namespace och $Location .

  • $Namespaceär namnet på den Service Bus namnrymd som vi vill arbeta med.
  • $Location identifierar det datacenter där vi etablerar namnområdet.
  • $CurrentNamespace lagrar det referensnamnområde som vi hämtar (eller skapar).

I ett faktiskt skript $Namespace och kan skickas som $Location parametrar.

Den här delen av skriptet gör följande:

  1. Försöker hämta en Service Bus med det angivna namnet.

  2. Om namnområdet hittas rapporterar det vad som hittades.

  3. Om namnområdet inte hittas skapas namnområdet och det nya namnområdet hämtas.

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

Skapa en auktoriseringsregel för namnområde

I följande exempel visas hur du hanterar auktoriseringsregler för namnområden med hjälp av cmdletarna New-AzServiceBusAuthorizationRule, Get-AzServiceBusAuthorizationRule, Set-AzServiceBusAuthorizationRuleoch 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
}

Skapa en kö

Om du vill skapa en kö eller ett ämne utför du en namnrymdskontroll med hjälp av skriptet i föregående avsnitt. Skapa sedan kön:

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

Ändra köegenskaper

När du har kör skriptet i föregående avsnitt kan du använda cmdleten Set-AzServiceBusQueue för att uppdatera egenskaperna för en kö, som i följande exempel:

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

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

Etablera andra Service Bus entiteter

Du kan använda powershell Service Bus modulen för att etablera andra entiteter, till exempel ämnen och prenumerationer. Dessa cmdlets liknar syntaktiskt cmdletarna för att skapa köer som visades i föregående avsnitt.

Nästa steg

Det finns några alternativa sätt att hantera Service Bus entiteter, enligt beskrivningen i följande blogginlägg: