在 Azure PowerShell 中实际部署之前获取 Azure 部署模板中定义的资源列表

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

在 Azure CLI 中,我可以使用 az deployment sub validate 获取有关订阅级部署模板中所有资源的详细输出:

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

az deployment sub validate 的 Azure PowerShell 等效项是 Test-AzSubscriptionDeployment,但 returns 仅错误(如果有),没有资源 ID 列表。

如何使用 Azure PowerShell 为模板定义的资源获取类似的资源 ID 列表?

这两个操作对 Azure 进行相同的 API 调用并得到相同的响应,但如您所见,Powershell only returns errors 而不是实际的 API 响应内容。

您可以通过手动调用 API 来解决,但有点麻烦。

$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