Azure – Retrieve Service Bus event hub connection string

arm-templateazureazure-powershellazure-resource-manager

I have an existing Service Bus with one queue and event hub deployed using Azure Resource Manager.

Now I am interested to retrieve the primary key and connection string using Azure PowerShell wiithout using the ServiceBus.dll. Is it possible??

As a workaround I have created an ARM template which does not deploy anything but just query the existing resource and retrieve the information I need. The below template retrieves the connection string and primary key of an event hub/queue for a specific service bus namespace

{
    "$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]"
        }
    }
}

Is it abusing to use ARM template to query for a resource and not actually deploy anything?

EDIT
To capture the output of the ARM template within PowerShell use the below code

$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

Note that the property names connectionString and primaryKey are same as defined in my template file

EDIT 2
If I re-run the ARM template to deploy the event hub second time I get the below error. enter image description here

I din't find any option other than to use the ARM template to query the details.

Best Answer

I don’t see what’s wrong with what you’re doing. In my view Resource Manager templates in their nature are incremental. So you could author a template to create your existing service bus with the same resources. If the properties are the same then it will leave the existing resources intact and return you the connection string and primary key of the relevant resource.

I have a need to automate the creation of a service bus and queue and separate send/listen shared access policies. You can retrieve the connection string on the service bus itself using PowerShell natively without using the .Net ServiceBus.dll assembly by using Get-AzureSBAuthorizationRule but due to a still current bug this doesn’t work at the queue level.

I tried using the ServiceBus.dll to create the shared access policies but sometimes it would randomly fail but subsequently work if you ran it a second time immediately afterwards. I also tried Resource Manager templates but previously you had to pass in the keys you’d generated yourself. Now I see Microsoft generate those for you but you’re still left trying to get the key in an automated fashion so I like your solution.

One question though, can you capture the Resource Manager template outputs and pass them back to a PowerShell script, do you know?

Cheers

Rob

{   "$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]"
    }   } }

But I call my templates like this:

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

Related Topic