如何使用 AZ CLI 尋找缺少特定標籤的 Azure 資源?

如何使用 AZ CLI 尋找缺少特定標籤的 Azure 資源?

我需要確保我們所有的資源組都有特定的標籤。

我知道我可以使用策略來確保任何建立的資源都具有標籤,但對於現有資源,我嘗試使用 AZ CLI 提出查詢。

作為額外的挑戰,該標籤的名稱中間有一個空格:它是“Cost Center”而不是“CostCenter”。 :-/

答案1

我建議使用Azure 資源圖為了這。資源圖可讓您在 CLI 或入口網站中使用 Kusto 語言查詢所有 Azure 資源。

若要尋找沒有「成本中心」標籤的所有資源群組的名稱,您可以執行以下查詢:

ResourceContainers 
| project name, type, tags 
| where type == 'microsoft.resources/subscriptions/resourcegroups' 
| where tags !contains 'Cost Center' 
| project name

從 CLI 中,此查詢將如下所示:

az graph query -q "ResourceContainers | project name, type, tags | where type == 'microsoft.resources/subscriptions/resourcegroups' | where tags !contains 'Cost Center' | project name"

相關內容