azure - 检索服务总线事件中心连接字符串

标签 azure azure-powershell azure-resource-manager azure-rm-template

我有一个现有的服务总线,其中有一个队列和使用 Azure 资源管理器部署的事件中心。

现在我有兴趣使用 Azure PowerShell 检索主键和连接字符串,而不使用 ServiceBus.dll。可能吗?

作为解决方法,我创建了一个 ARM 模板,该模板不部署任何内容,而只是查询现有资源并检索我需要的信息。下面的模板检索特定服务总线命名空间的事件中心/队列的连接字符串和主键

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "serviceBusNamespace": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "The name of the service bus namespace to create."
            }
        },
        "resourceName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "The name of the resource to be retreived."
            }
        },
        "resourceType": {
            "type": "string",
            "minLength": 1,
            "allowedValues": [
                "queues",
                "eventhubs"
            ],
            "metadata": {
                "description": "The type of the resource"
            }
        },
        "policy": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "ManagePolicy",
            "allowedValues": [
                "ManagePolicy",
                "SendPolicy",
                "ListenPolicy"
            ],
            "metadata": {
                "description": "The type of the resource"
            }
        }
    },
    "variables": {
    },
    "resources": [ ],
    "outputs": {
        "connectionString": {
            "type": "string",
            "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/',parameters('resourceType'),'/authorizationRules'),parameters('serviceBusNamespace'),parameters('resourceName'),parameters('policy')),'2015-08-01').primaryConnectionString]"
        },
        "primaryKey": {
            "type": "string",
            "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/',parameters('resourceType'),'/authorizationRules'),parameters('serviceBusNamespace'),parameters('resourceName'),parameters('policy')),'2015-08-01').primaryKey]"
        }
    }
}

使用 ARM 模板查询资源而不实际部署任何内容是否属于滥用行为?

编辑 要在 PowerShell 中捕获 ARM 模板的输出,请使用以下代码

$ep = New-AzureRmResourceGroupDeployment -Name "getEventHub" -ResourceGroupName myResourceGroup -Mode Incremental -TemplateFile getEventHub.json -TemplateParameterFile getEventHub.param.json
$RuleConnString = $ep.Outputs.connectionString.value
$RulePrimaryKey = $ep.Outputs.primaryKey.value

请注意,属性名称 connectionStringprimaryKey 与我的模板文件中定义的相同

编辑2 如果我第二次重新运行 ARM 模板来部署事件中心,则会收到以下错误。 enter image description here

除了使用ARM模板查询详细信息之外,我没有找到任何其他选项。

最佳答案

我不明白你所做的有什么问题。在我看来,资源管理器模板本质上是增量的。因此,您可以编写一个模板来使用相同的资源创建现有的服务总线。如果属性相同,那么它将保持现有资源不变,并返回相关资源的连接字符串和主键。

我需要自动创建服务总线和队列以及单独的发送/监听共享访问策略。您可以使用 PowerShell native 检索服务总线本身上的连接字符串,而无需使用 Get-AzureSBAuthorizationRule 来使用 .Net ServiceBus.dll 程序集,但由于仍然存在错误,这在队列级别不起作用。

我尝试使用 ServiceBus.dll 创建共享访问策略,但有时它会随机失败,但如果您随后立即运行第二次,它随后会工作。我还尝试了资源管理器模板,但之前您必须传递自己生成的 key 。现在,我看到 Microsoft 为您生成了这些 key ,但您仍然需要尝试以自动方式获取 key ,所以我喜欢您的解决方案。

有一个问题,您知道吗?您可以捕获资源管理器模板输出并将其传递回 PowerShell 脚本吗?

干杯

{   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0",   "parameters": {
    "servicebusNamespace": {
      "type": "string",
      "metadata": {
        "description": "The service bus namespace"
      }
    },
    "notificationssmsqueue": {
      "type": "string",
      "metadata": {
        "description": "Notifications SMS queue"
      }
    }   },   "variables": {
    "location": "[resourceGroup().location]",   },   "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[parameters('servicebusNamespace')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[variables('location')]",
      "properties": {
        "messagingSku": 2
      },
      "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "[parameters('notificationssmsqueue')]",
          "type": "Queues",
          "dependsOn": [
            "[concat('Microsoft.ServiceBus/namespaces/', parameters('servicebusNamespace'))]"
          ],
          "properties": {
            "path": "[parameters('notificationssmsqueue')]"
          },
          "resources": [
            {
              "apiVersion": "2015-08-01",
              "name": "[concat(parameters('notificationssmsqueue'),'.listen')]",
              "type": "AuthorizationRules",
              "dependsOn": [
                "[parameters('notificationssmsqueue')]"
              ],
              "properties": {
                "keyName": "[concat(parameters('notificationssmsqueue'),'.listen')]",
                "claimType": "SharedAccessKey",
                "claimValue": "None",
                "rights": [ "Listen" ],
                "revision": -1
              }
            },
            {
              "apiVersion": "2015-08-01",
              "name": "[concat(parameters('notificationssmsqueue'),'.send')]",
              "type": "AuthorizationRules",
              "dependsOn": [
                "[parameters('notificationssmsqueue')]"
              ],
              "properties": {
                "keyName": "[concat(parameters('notificationssmsqueue'),'.send')]",
                "claimType": "SharedAccessKey",
                "claimValue": "None",
                "rights": [ "Send" ],
                "revision": -1
              }
            }
          ]
        }
      ]
    }   ],   "outputs": {
    "connectionString": {
      "type": "string",
      "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/AuthorizationRules'),parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]"
    },
    "smsSendPrimaryKey": {
      "type": "string",
      "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/Queues/AuthorizationRules'),parameters('serviceBusNamespace'),parameters('notificationssmsqueue'),concat(parameters('notificationssmsqueue'),'.send')),'2015-08-01').PrimaryKey]"
    },
    "smsListenPrimaryKey": {
      "type": "string",
      "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/Queues/AuthorizationRules'),parameters('serviceBusNamespace'),parameters('notificationssmsqueue'),concat(parameters('notificationssmsqueue'),'.listen')),'2015-08-01').PrimaryKey]"
    }   } }

但我这样称呼我的模板:

New-AzureRMResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile "$scripts_folder$SB_create_script"-TemplateParameterObject ` @{ servicebusNamespace = $servicebusNamespace; notificationsmsqueue = $NotificationSMSqueue }

关于azure - 检索服务总线事件中心连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299640/

相关文章:

android - Azure 媒体服务的 MPEG-DASH 中出现错误

azure - "Content not available"Power BI 嵌入带有 azure 身份验证 token 的 ionic 应用程序

powershell - VSTS Azure powershell : No default subscription has been designated

azure - 从 Bicep 中的符号名称数组获取资源

azure - 在 Azure 中创建新角色分配时出错

azure - 找到有针对我的评论的工作项目?

asp.net - 如何将 .lic 文件发布(部署)到 Azure Web 应用服务的根目录?

azure powershell 集成帐户合作伙伴创建身份无效

powershell - 在从 VSTS 进行新部署之前删除 Azure 上的文件和文件夹

azure - 使用 Bicep 输出新创建的功能应用程序 key