Fabian Schmied
Fabian Schmied

Reputation: 4164

Get a list of resources defined in an Azure deployment template prior to actual deployment in Azure PowerShell

In Azure CLI, I can use az deployment sub validate to get detailed output about all the resources in a subscription-level deployment template:

{
  "error": null,
  "id": "/subscriptions/.../providers/Microsoft.Resources/deployments/main",
  "location": "westeurope",
  "name": "main",
  "properties": {
    ...
    "validatedResources": [
      {
        "id": "/subscriptions/.../resourceGroups/my-rg"
      },
      {
        "id": "/subscriptions/.../resourceGroups/my-rg/providers/Microsoft.Resources/deployments/logAnalyticsWorkspace",
        "resourceGroup": "my-rg"
      },
      {
        "id": "/subscriptions/.../resourceGroups/my-rg/providers/Microsoft.Resources/deployments/identity",
        "resourceGroup": "my-rg"
      },
      ...
    ]
  },
  "type": "Microsoft.Resources/deployments"
}

The Azure PowerShell equivalent of az deployment sub validate is Test-AzSubscriptionDeployment, but that returns only errors (if any), no list of resource IDs.

How can I get a similar list of resource IDs for the resources defined by a template using Azure PowerShell?

Upvotes: 1

Views: 566

Answers (1)

kwill
kwill

Reputation: 11008

Both of those operations make the same API call to Azure and get the same response back, but as you found, Powershell only returns errors and not the actual API response content.

You can work around by manually calling the API, but it is a bit cumbersome.

$Template = Get-Content -Path template.json | ConvertFrom-Json
$payload = @{location="westus2"; properties=@{template = $Template; mode="Incremental"}} | ConvertTo-Json -Depth 20
$results = Invoke-AzRestMethod -Path "/subscriptions/$subid/providers/Microsoft.Resources/deployments/test/validate?api-version=2021-04-01" -Method POST -Payload $payload
($results.Content | ConvertFrom-Json).properties.validatedResources

Upvotes: 2

Related Questions