通过 Powershell 检索 Azure 服务总线共享访问连接字符串

Retrieve Azure Service Bus Shared Access Connection String via Powershell

可以使用 Powershell 中的 Get-AzureRmStorageAccountKey 检索 Azure 存储帐户的访问密钥。我如何获得 Azure 服务总线的共享访问策略的访问密钥?

更多说明

这是我在使用 Get-AzureRmServiceBusNamespaceKey cmdlet 时得到的结果:

PS C:\Windows\system32> Login-AzureRmAccount -Credential $cred

Environment           : AzureCloud
Account               : ***redacted***
TenantId              : ***redacted***
SubscriptionId        : ***redacted***
CurrentStorageAccount : 

PS C:\Windows\system32> Set-AzureRmContext -SubscriptionId ***redacted***

Environment           : AzureCloud
Account               : ***redacted***
TenantId              : ***redacted***
SubscriptionId        : ***redacted***
CurrentStorageAccount : 


PS C:\Windows\system32> Get-AzureRmServiceBusNamespaceKey -ResourceGroup testresourcegroup -Name test-bus -AuthorizationRuleName SendPolicy
Get-AzureRmServiceBusNamespaceKey : Run Login-AzureRmAccount to login.
At line:1 char:1
+ Get-AzureRmServiceBusNamespaceKey -ResourceGroup testresourcegroup -Name    test-bus ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Get-AzureRmServiceBusNamespaceKey], PSInvalidOperationException
+ FullyQualifiedErrorId :   InvalidOperation,Microsoft.Azure.Commands.ServiceBus.Commands.Namespace.GetAzure RmServiceBusNamespaceKey


PS C:\Windows\system32> Get-AzureRmStorageAccountKey -ResourceGroupName testresourcegroup -Name teststoragexxx

Key1                                                                                                        Key2                                                                                                   
----                                                                                                    ----                                                                                                   
***redacted***                ***redacted***

不适用于 Get-AzureRmStorageAccountKey,但您可以使用 Get-AzureRmServiceBusNamespaceKey

$resourceGroup = "myResourceGroup"
$serviceBusName ="myservicebusname"
$policyName = "policyname"

Get-AzureRmServiceBusNamespaceKey -ResourceGroup $resourceGroup -Name $serviceBusName -AuthorizationRuleName $policyName

这将 return 整个对象,因此您可以将其传递给变量并从中获取键或连接字符串。

请尝试使用 tenantId 和 ServicePrincipal 登录。我对此进行了演示测试,它对我来说工作正常。

Login-AzureRmAccount -Credential $psCred  -TenantId $azureTenantId  -ServicePrincipal -SubscriptionId $subscriptionId 

以下是详细步骤。

1。如果没有安装,我们需要安装服务总线模块。更多关于AzureRM.ServiceBus的详细信息请参考document

Install-Module -Name AzureRM.ServiceBus 

2.More关于自动登录脚本的详细信息请参考另一个

3.Run测试脚本并检查结果。

$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$subscriptionId="Your subcription"
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Login-AzureRmAccount -Credential $psCred  -TenantId $azureTenantId  -ServicePrincipal -SubscriptionId $subscriptionId 


$resourceGroup = "Resource Group name"
$serviceBusName ="Service Bus Name"
$policyName = "Policy Name"
Get-AzureRmServiceBusNamespaceKey  -ResourceGroup $resourceGroup -Name $serviceBusName -AuthorizationRuleName $policyName