Tenant and Subscription display names now available in Azure Resource Graph command lines
I love Azure Resource Graph but one of the things I deeply wanted since its launch is a native way to get the subscription or tenant names, not just the id. Good news! It's now possible if you use Azure CLI or Azure PowerShell.
Prerequisites
The following article has been tested with:
- Azure CLI:
resource-graph
extension v1.0.0 - PowerShell:
Az.ResourceGraph
module v0.7.6
Before the include
Here's a simple query that returns only one result
#Azure CLI
az graph query -q "where type =~ 'Microsoft.Network/dnsZones' | limit 1"
# PowerShell
Search-AzGraph -Query "where type =~ 'Microsoft.Network/dnsZones' | limit 1"
Both will return the same data, let's convert it to JSON, we end up with the following output:
[
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ciah-shared-all/providers/Microsoft.Network/dnszones/poc.ciah.dev",
"identity": null,
"kind": "",
"location": "global",
"managedBy": "",
"name": "poc.ciah.dev",
"plan": null,
"properties": {
"maxNumberOfRecordSets": 10000,
"maxNumberOfRecordsPerRecordSet": null,
"nameServers": [
"ns1-03.azure-dns.com.",
"ns2-03.azure-dns.net.",
"ns3-03.azure-dns.org.",
"ns4-03.azure-dns.info."
],
"numberOfRecordSets": 5,
"zoneType": "Public"
},
"resourceGroup": "ciah-shared-all",
"sku": null,
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"tags": {},
"tenantId": "00000000-0000-0000-0000-000000000000",
"type": "microsoft.network/dnszones",
"zones": null
}
]
As you can see, we get a member subscriptionId
and tenantId
but no names.
Welcome the include parameter
In the latest versions of Azure Resource Graph PowerShell module (PS)/extension (CLI), you now have the option to include this information automatically. If you DON'T use projection in your query using the project
operator, you can do something like this:
#Azure CLI
az graph query -q "where type =~ 'Microsoft.Network/dnsZones' | limit 1" --include displayNames
# PowerShell
Search-AzGraph -Query "where type =~ 'Microsoft.Network/dnsZones' | limit 1" -Include displayNames
Will result in:
[
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ciah-shared-all/providers/Microsoft.Network/dnszones/poc.ciah.dev",
"identity": null,
"kind": "",
"location": "global",
"managedBy": "",
"name": "poc.ciah.dev",
"plan": null,
"properties": {
"maxNumberOfRecordSets": 10000,
"maxNumberOfRecordsPerRecordSet": null,
"nameServers": [
"ns1-03.azure-dns.com.",
"ns2-03.azure-dns.net.",
"ns3-03.azure-dns.org.",
"ns4-03.azure-dns.info."
],
"numberOfRecordSets": 5,
"zoneType": "Public"
},
"resourceGroup": "ciah-shared-all",
"sku": null,
"subscriptionDisplayName": "MSDN Subscription",
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"tags": {},
"tenantDisplayName": "POC",
"tenantId": "00000000-0000-0000-0000-000000000000",
"type": "microsoft.network/dnszones",
"zones": null
}
]
If you DO use projection, ensure you specify which display name you want returned, i.e.:
#Azure CLI
az graph query -q "where type =~ 'Microsoft.Network/dnsZones' | limit 1 | project name, resourceGroup, subscriptionDisplayName, tenantDisplayName" --include displayNames
# PowerShell
Search-AzGraph -Query "where type =~ 'Microsoft.Network/dnsZones' | limit 1 | project name, resourceGroup, subscriptionDisplayName, tenantDisplayName" -Include displayNames
Will result in:
[
{
"name": "poc.ciah.dev",
"resourceGroup": "ciah-shared-all",
"subscriptionDisplayName": "MSDN Subscription",
"tenantDisplayName": "POC"
}
]
Hope it helps you if you use the command line to query Azure Resource Graph.
References
- Azure Resource Graph
- Run your first Resource Graph query using Azure CLI
- Run your first Resource Graph query using Azure PowerShell
- Kusto Project operator