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 Service Bus Resource Manager PowerShell-modulen för att etablera och hantera Service Bus-entiteter (namnområden, köer, ämnen och prenumerationer) med hjälp av en lokal Azure PowerShell-konsol eller skript.

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

Anteckning

Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Se Installera Azure PowerShell för att komma igång. 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 krav:

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 anvisningarna i Kom igång med Azure PowerShell cmdletar 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-AzServiceBusNamespace och Set-AzServiceBusNamespace .

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

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

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

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

  1. Försöker hämta ett Service Bus-namnområde med det angivna namnet.

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

  3. Om namnområdet inte hittas skapar det namnområdet och hämtar sedan det nya namnområdet.

     # 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 cmdletarna New-AzServiceBusAuthorizationRule, Get-AzServiceBusAuthorizationRule, Set-AzServiceBusAuthorizationRule och 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 namnområdeskontroll 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
    $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ört 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 Service Bus PowerShell-modulen för att etablera andra entiteter, till exempel ämnen och prenumerationer. Dessa cmdletar liknar cmdletarna för att skapa köer syntaktiskt som visas 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: