Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
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 ett lokalt 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.
Kommentar
Vi rekommenderar att du använder Azure Az PowerShell-modulen för att interagera med Azure. Information om hur du kommer igång finns i 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 krav:
- En Azure-prenumeration Mer information om hur du skaffar en prenumeration finns i köpalternativ, medlemserbjudanden eller kostnadsfritt konto.
- En dator med Azure PowerShell. Anvisningar finns i Komma igång med Azure PowerShell-cmdletar.
- En allmän förståelse för PowerShell-skript, NuGet-paket och .NET Framework.
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.
I det här exemplet skapas några lokala variabler i skriptet. $Namespace och $Location.
$Namespaceär namnet på Service Bus-namnområdet som vi vill arbeta med.$Locationidentifierar det datacenter där vi etablerar namnområdet.$CurrentNamespacelagrar det referensnamnområde 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:
Försöker hämta ett Service Bus-namnområde med det angivna namnet.
Om namnområdet hittas rapporterar det vad som hittades.
Om namnområdet inte hittas skapar det namnområdet och hämtar sedan det nyligen skapade 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 hjälp av cmdletarna New-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 syntaktiskt cmdletarna för att skapa köer som visades i föregående avsnitt.
Nästa steg
- Se den fullständiga Dokumentationen om Service Bus Resource Manager PowerShell-modulen här. På den här sidan visas alla tillgängliga cmdletar.
- Information om hur du använder Azure Resource Manager-mallar finns i artikeln Skapa Service Bus-resurser med hjälp av Azure Resource Manager-mallar.
- Information om Service Bus .NET-hanteringsbibliotek.
Det finns några alternativa sätt att hantera Service Bus-entiteter enligt beskrivningen i följande blogginlägg: