Home > Software engineering >  Use ARM functions from PowerShell
Use ARM functions from PowerShell

Time:11-10

TL;DR

The following line works fine as part of an ARM script ran in an Azure DevOps ARM Template Deployment task but, when ran from PowerShell, it fails to pull the subscription and the resource group.

"serverFarmId": "[concat('/subscriptions/', subscription(), '/resourcegroups/', resourceGroup(), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"

how can I set the subscription and resource group variables so the ARM functions can pull the values?

P.S. It's ok If I have to first get them and then set them manually for the script to consume, as long as I can use the same script for DevOps and PowerShell.


Here's the resources section of the template which, again, when deployed from a pipeline it pulls the subscription and the resource group from whatever is set in the task parameters.

"resources": [
{
  "apiVersion": "2018-11-01",
  "name": "[parameters('name')]",
  "type": "Microsoft.Web/sites",
  "location": "[resourceGroup().location]",
  "tags": {},
  "dependsOn": [
    "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
  ],
  "properties": {
    "name": "[parameters('name')]",
    "siteConfig": {
      "metadata": [
        {
          "name": "CURRENT_STACK",
          "value": "[parameters('currentStack')]"
        }
      ]
    },
    "serverFarmId": "[concat('/subscriptions/', subscription(), '/resourcegroups/', resourceGroup(), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
    "clientAffinityEnabled": true
  }
},
{
  "apiVersion": "2018-11-01",
  "name": "[parameters('hostingPlanName')]",
  "type": "Microsoft.Web/serverfarms",
  "location": "[resourceGroup().location]",
  "kind": "",
  "tags": {},
  "dependsOn": [],
  "properties": {
    "name": "[parameters('hostingPlanName')]"
  },
  "sku": {
    "Tier": "[parameters('sku')]",
    "Name": "[parameters('skuCode')]"
  }
}
]

CodePudding user response:

subscription() and resourceGroup() do return objects based on your context. You should consider querying attributes instead like : subscription().subscriptionId and resourceGroup().name considering how you are constructing your ID. I am not sure why it works in your ARM template though.

As a side comment, you could make use of the resourceId([subscriptionId], [resourceGroupName], resourceType, resourceName1, [resourceName2], ...) function as explained here, under the hood it performs the same as you did (concat).

  • Related