Search-AzGraph (Az.ResourceGraph) imposes more stringing JOIN and mv-expand limits compared to Azure ARM portal
The following complex query returns a list of all virtual machines with the associated Network Security Groups (whether it indirect via subnet or via each attached network interface). The query works great in the Azure ARM portal but does NOT work in any current version (5.1 or 7.1) of PowerShell using the Search-AzGraph cmdlet due to more stringent limits. This is frustrating for people who develop and debug complex queries via the portal -- only to find they won't work in Search-AzGraph. I suspect this may be that the code needs to call a newer version of the underlying REST API.
Steps to reproduce
Invoke the following in PowerShell and it will fail:
$query = 'Resources
| where type =~ "microsoft.network/networkinterfaces"
| mv-expand ipConfigurations = properties.ipConfigurations
| extend privateIPType = tostring(ipConfigurations.properties["privateIPAllocationMethod"])
| extend privateIP = tostring(ipConfigurations.properties["privateIPAddress"])
| extend subnetId = tostring(ipConfigurations.properties.subnet["id"])
| extend publicIPid = tostring(ipConfigurations.properties["publicIPAddress"].id)
| extend nicId = tostring(id)
| join kind=leftouter (Resources
| where type contains "publicIPAddresses" and isnotempty(properties.ipAddress)
| extend publicIP = tostring(properties.ipAddress),
publicIPid = tostring(id)) on publicIPid
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand nics = properties.networkInterfaces
| extend nicId = tostring (nics.id),
nicNSG = name,
nicNSGid = id,
nicNSGgroup = resourceGroup ) on nicId
| join kind=leftouter (Resources
| where type == "microsoft.compute/virtualmachines" and isnotempty(properties.networkProfile.networkInterfaces)
| extend vmName = name
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend osType = tostring(properties.storageProfile.osDisk.osType)
| mv-expand nics = properties.networkProfile.networkInterfaces
| extend nicId = tostring (nics.id) ) on nicId
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand subnets = properties.subnets
| extend subnetId = tostring(subnets.id),
vnetName = split(tostring(subnets.id),"/")[8],
subnetName = split(tostring(subnets.id),"/")[10],
subnetNSG = name,
subnetNSGid = id,
subnetNSGgroup = resourceGroup
) on subnetId
| project subscriptionId, nicName=name, resourceGroup, vmName, vmSize, osType, vnetName, subnetName, nicNSG, subnetNSG, location, privateIPType, privateIP, publicIP, tags, subnetId, nicId, nicNSGid, subnetNSGid'
# Run the query
$results = Search-AzGraph -Query $query
Cut and paste the query itself into the Azure Resource Graph Explorer at https://portal.azure.com/#blade/HubsExtension/ArgQueryBlade and it works fine.
Expected behavior
Search-AzGraph should execute any query that one can construct and run successfully in the Azure Resource Graph explorer.
Actual behavior
Two errors are thrown indicating too many mvexpand columns and too many JOINs:
Search-AzGraph : {
"error": {
"code": "BadRequest",
"message": "Please provide below info when asking for support: timestamp = 2020-12-27T16:36:57.6771083Z, correlationId = 41972177-8eae-4e52-b2a4-40baaae80ff8.",
"details": [
{
"code": "InvalidQuery",
"message": "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."
},
{
"code": "TooManyMvExpandColumns",
"message": "The query invokes mvexpand on 4 columns, and has exceeded the limit of 2. Please consider simplifying the query logic."
},
{
"code": "TooManyJoins",
"message": "The query uses 4 join operators, and has exceeded the limit of 3. Please consider simplfying the query logic."
}
]
}
}
At line:1 char:12
+ $results = Search-AzGraph -Query $query
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Search-AzGraph], ErrorResponseException
+ FullyQualifiedErrorId : BadRequest,Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph
Environment data
Windows 10 desktop with PowerShell 7
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Work-Around
No work-around is known at this time.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @chiragg4u.
Issue Details
The following complex query returns a list of all virtual machines with the associated Network Security Groups (whether it indirect via subnet or via each attached network interface). The query works great in the Azure ARM portal but does NOT work in any current version (5.1 or 7.1) of PowerShell using the Search-AzGraph cmdlet due to more stringent limits. This is frustrating for people who develop and debug complex queries via the portal -- only to find they won't work in Search-AzGraph. I suspect this may be that the code needs to call a newer version of the underlying REST API.
Steps to reproduce
Invoke the following in PowerShell and it will fail:
$query = 'Resources
| where type =~ "microsoft.network/networkinterfaces"
| mv-expand ipConfigurations = properties.ipConfigurations
| extend privateIPType = tostring(ipConfigurations.properties["privateIPAllocationMethod"])
| extend privateIP = tostring(ipConfigurations.properties["privateIPAddress"])
| extend subnetId = tostring(ipConfigurations.properties.subnet["id"])
| extend publicIPid = tostring(ipConfigurations.properties["publicIPAddress"].id)
| extend nicId = tostring(id)
| join kind=leftouter (Resources
| where type contains "publicIPAddresses" and isnotempty(properties.ipAddress)
| extend publicIP = tostring(properties.ipAddress),
publicIPid = tostring(id)) on publicIPid
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand nics = properties.networkInterfaces
| extend nicId = tostring (nics.id),
nicNSG = name,
nicNSGid = id,
nicNSGgroup = resourceGroup ) on nicId
| join kind=leftouter (Resources
| where type == "microsoft.compute/virtualmachines" and isnotempty(properties.networkProfile.networkInterfaces)
| extend vmName = name
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend osType = tostring(properties.storageProfile.osDisk.osType)
| mv-expand nics = properties.networkProfile.networkInterfaces
| extend nicId = tostring (nics.id) ) on nicId
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand subnets = properties.subnets
| extend subnetId = tostring(subnets.id),
vnetName = split(tostring(subnets.id),"/")[8],
subnetName = split(tostring(subnets.id),"/")[10],
subnetNSG = name,
subnetNSGid = id,
subnetNSGgroup = resourceGroup
) on subnetId
| project subscriptionId, nicName=name, resourceGroup, vmName, vmSize, osType, vnetName, subnetName, nicNSG, subnetNSG, location, privateIPType, privateIP, publicIP, tags, subnetId, nicId, nicNSGid, subnetNSGid'
# Run the query
$results = Search-AzGraph -Query $query
Cut and paste the query itself into the Azure Resource Graph Explorer at https://portal.azure.com/#blade/HubsExtension/ArgQueryBlade and it works fine.
Expected behavior
Search-AzGraph should execute any query that one can construct and run successfully in the Azure Resource Graph explorer.
Actual behavior
Two errors are thrown indicating too many mvexpand columns and too many JOINs:
Search-AzGraph : {
"error": {
"code": "BadRequest",
"message": "Please provide below info when asking for support: timestamp = 2020-12-27T16:36:57.6771083Z, correlationId = 41972177-8eae-4e52-b2a4-40baaae80ff8.",
"details": [
{
"code": "InvalidQuery",
"message": "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."
},
{
"code": "TooManyMvExpandColumns",
"message": "The query invokes mvexpand on 4 columns, and has exceeded the limit of 2. Please consider simplifying the query logic."
},
{
"code": "TooManyJoins",
"message": "The query uses 4 join operators, and has exceeded the limit of 3. Please consider simplfying the query logic."
}
]
}
}
At line:1 char:12
+ $results = Search-AzGraph -Query $query
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Search-AzGraph], ErrorResponseException
+ FullyQualifiedErrorId : BadRequest,Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph
Environment data
Windows 10 desktop with PowerShell 7
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Work-Around
No work-around is known at this time.
| Author: | lesterw1 |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Resource Graph team
Portal is using API version 2018-09-01-preview. But it doesn't work even I call its REST API directly
$payload = @{}
$payload.Add("subscriptions", @("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))
$query = '
Resources
| where type =~ "microsoft.network/networkinterfaces"
| mv-expand ipConfigurations = properties.ipConfigurations
| extend privateIPType = tostring(ipConfigurations.properties["privateIPAllocationMethod"])
| extend privateIP = tostring(ipConfigurations.properties["privateIPAddress"])
| extend subnetId = tostring(ipConfigurations.properties.subnet["id"])
| extend publicIPid = tostring(ipConfigurations.properties["publicIPAddress"].id)
| extend nicId = tostring(id)
| join kind=leftouter (Resources
| where type contains "publicIPAddresses" and isnotempty(properties.ipAddress)
| extend publicIP = tostring(properties.ipAddress),
publicIPid = tostring(id)) on publicIPid
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand nics = properties.networkInterfaces
| extend nicId = tostring (nics.id),
nicNSG = name,
nicNSGid = id,
nicNSGgroup = resourceGroup ) on nicId
| join kind=leftouter (Resources
| where type == "microsoft.compute/virtualmachines" and isnotempty(properties.networkProfile.networkInterfaces)
| extend vmName = name
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend osType = tostring(properties.storageProfile.osDisk.osType)
| mv-expand nics = properties.networkProfile.networkInterfaces
| extend nicId = tostring (nics.id) ) on nicId
'
$payload["query"] = $query
Invoke-AzRestMethod -Path /providers/Microsoft.ResourceGraph/resources?api-version=2018-09-01-preview -Method POST -Payload (ConvertTo-Json $payload)
But it works once I removed one join clause
$payload = @{}
$payload.Add("subscriptions", @("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"))
$query = '
Resources
| where type =~ "microsoft.network/networkinterfaces"
| mv-expand ipConfigurations = properties.ipConfigurations
| extend privateIPType = tostring(ipConfigurations.properties["privateIPAllocationMethod"])
| extend privateIP = tostring(ipConfigurations.properties["privateIPAddress"])
| extend subnetId = tostring(ipConfigurations.properties.subnet["id"])
| extend publicIPid = tostring(ipConfigurations.properties["publicIPAddress"].id)
| extend nicId = tostring(id)
| join kind=leftouter (Resources
| where type contains "publicIPAddresses" and isnotempty(properties.ipAddress)
| extend publicIP = tostring(properties.ipAddress),
publicIPid = tostring(id)) on publicIPid
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand nics = properties.networkInterfaces
| extend nicId = tostring (nics.id),
nicNSG = name,
nicNSGid = id,
nicNSGgroup = resourceGroup ) on nicId
'
$payload["query"] = $query
Invoke-AzRestMethod -Path /providers/Microsoft.ResourceGraph/resources?api-version=2018-09-01-preview -Method POST -Payload (ConvertTo-Json $payload)
I also tried the various API versions and it fails. Adding to the curiosity though is that query WORKS when using the Azure AZ command line tool:
# This query WORKS when used via the AZ command line tool:
PS C:\> $query = 'Resources
| where type =~ "microsoft.network/networkinterfaces"
| mv-expand ipConfigurations = properties.ipConfigurations
| extend privateIPType = tostring(ipConfigurations.properties["privateIPAllocationMethod"])
| extend privateIP = tostring(ipConfigurations.properties["privateIPAddress"])
| extend subnetId = tostring(ipConfigurations.properties.subnet["id"])
| extend publicIPid = tostring(ipConfigurations.properties["publicIPAddress"].id)
| extend nicId = tostring(id)
| join kind=leftouter (Resources
| where type contains "publicIPAddresses" and isnotempty(properties.ipAddress)
| extend publicIP = tostring(properties.ipAddress),
publicIPid = tostring(id)) on publicIPid
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand nics = properties.networkInterfaces
| extend nicId = tostring (nics.id),
nicNSG = name,
nicNSGid = id,
nicNSGgroup = resourceGroup ) on nicId
| join kind=leftouter (Resources
| where type == "microsoft.compute/virtualmachines" and isnotempty(properties.networkProfile.networkInterfaces)
| extend vmName = name
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend osType = tostring(properties.storageProfile.osDisk.osType)
| mv-expand nics = properties.networkProfile.networkInterfaces
| extend nicId = tostring (nics.id) ) on nicId
| join kind=leftouter (Resources | where type == "microsoft.network/networksecuritygroups"
| mv-expand subnets = properties.subnets
| extend subnetId = tostring(subnets.id),
vnetName = split(tostring(subnets.id),"/")[8],
subnetName = split(tostring(subnets.id),"/")[10],
subnetNSG = name,
subnetNSGid = id,
subnetNSGgroup = resourceGroup
) on subnetId
| project subscriptionId, nicName=name, resourceGroup, vmName, vmSize, osType, vnetName, subnetName, nicNSG, subnetNSG, location, privateIPType, privateIP, publicIP, tags, subnetId, nicId, nicNSGid, subnetNSGid'
# AZ tool output:
PS C:\> $r = az graph query -q $query
PS C:\> $r1 = $r | ConvertFrom-json
PS C:\> $r1.count
84
PS C:\> $r1[0] # 1st entry:
extendedLocation :
id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/DataCatalogs-EastUS/providers/Microsoft.DataCatalog/catalogs/IoTahoe
identity :
kind :
location : eastus
managedBy :
name : MyDC
plan :
properties : @{admins=System.Object[]; enableAutomaticBillingUnitAdjustment=False; provisioningState=Succeeded; sku=Standard; users=System.Object[]}
resourceGroup : datacatalogs-eastus
sku :
subscriptionId : 00000000-0000-0000-0000-000000000000
tags : @{CreatedBy=xxxxxxxx; CreatedTime=11/21/2020 01:28:24}
tenantId : 00000000-0000-1111-2222-000000000000
type : microsoft.datacatalog/catalogs
zones :
# Version of AZ tool on Windows 10 Desktop:
PS C:\> az version
{
"azure-cli": "2.15.1",
"azure-cli-core": "2.15.1",
"azure-cli-telemetry": "1.0.6",
"extensions": {
"resource-graph": "1.1.0"
}
}
# But the SAME $query FAILS in PowerShell Search-AzGraph:
PS C:\> Search-AzGraph -Query $query
Search-AzGraph : {
"error": {
"code": "BadRequest",
"message": "Please provide below info when asking for support: timestamp = 2020-12-28T14:05:36.2981641Z, correlationId = c20308c2-a874-4fe1-a063-8a4f963a2378.",
"details": [
{
"code": "InvalidQuery",
"message": "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."
},
{
"code": "TooManyMvExpandColumns",
"message": "The query invokes mvexpand on 4 columns, and has exceeded the limit of 2. Please consider simplifying the query logic."
},
{
"code": "TooManyJoins",
"message": "The query uses 4 join operators, and has exceeded the limit of 3. Please consider simplfying the query logic."
}
]
}
}
At line:1 char:1
+ Search-AzGraph -Query $query
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Search-AzGraph], ErrorResponseException
+ FullyQualifiedErrorId : BadRequest,Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph
PS C:\>
On inspection of the Azure CLI source code to see what API it is using, it appears to call /providers/Microsoft.ResourceGraph/resources with API-version=2019-04-01.
So I tested with this URL: https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01
It fails with the same limitation errors BUT in addition to the error, this version also returns results, albeit incorrect – the same incorrect results that the Azure CLI returns (but without an error). Its odd that the Azure CLI tool appears to ignore the error (Azure CLI resource-graph extension bug https://github.com/Azure/azure-cli-extensions/issues/2844 opened). Note that the Azure ARM portal does return the correct results.
AZ CLI resource-graph Extension Code:
https://github.com/Azure/azure-cli-extensions/blob/master/src/resource-graph/azext_resourcegraph/custom.py
Thanks for investigation. I added service team member to watch this issue. Let's wait their further response.
Thanks for the investigations ARG has by design limited CLI access as its very easy to burn the query quota through CLI. You can script the above scenario by joining the data on client side. We will continue to enhance the CLI support, Please provide feedback in our uservoice channel so that we can prioritize most important features @ https://feedback.azure.com/forums/915958-azure-governance?category_id=345061