-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query works but Search-AzGraph
in Az.ResourceGraph
v1.0.0 does not manage to parse the output
#26470
Comments
Might be relevant: I started researching changing out PS > ConvertFrom-Json -InputObject $Response.'content'
ConvertFrom-Json: The provided JSON includes a property whose name is an empty string, this is only supported using the -AsHashTable switch.
PS > So the JSON response from the Azure RM API clearly contains a "property whose name is an empty string". Final workaround (it's not pretty, but it works)# Assets
$Resources = [System.Collections.Generic.List[System.Management.Automation.OrderedHashtable]]::new()
Remove-Variable -Name 'Response' -Force -ErrorAction 'Ignore'
$Query = [string] 'resources
| where type in~ ("Microsoft.Automation/automationAccounts","Microsoft.DataFactory/factories","Microsoft.DocumentDB/databaseAccounts","Microsoft.Logic/workflows","Microsoft.KeyVault/vaults","Microsoft.Network/bastionHosts","Microsoft.Network/networkSecurityGroups","Microsoft.Network/publicIPAddresses","Microsoft.Network/virtualNetworkGateways","Microsoft.ServiceBus/namespaces")
// Add tags from resource group
| join kind = leftouter (
resourcecontainers
| where type == "microsoft.resources/subscriptions/resourcegroups"
| project rgName = name, rgSubscriptionId = subscriptionId, rgTags = tags
) on $left.resourceGroup == $right.rgName and $left.subscriptionId == $right.rgSubscriptionId
| project-away rgName, rgSubscriptionId
// Add tags from subscription
| join kind = leftouter (
resourcecontainers
| where type == "microsoft.resources/subscriptions"
| project subName = name, subId = subscriptionId, subTags = tags
) on $left.subscriptionId == $right.subId
| project-away subId'
# Run resource graph query and handle paging
do {
$Body = [ordered]@{
'query' = [string] $ResourceGraphQuery
'options' = [ordered]@{
'$top' = [byte] 100
'resultFormat' = [string] 'objectArray'
'allowPartialScopes' = [bool] $false
}
}
if (-not [string]::IsNullOrEmpty($Content.'$skipToken')) {
$Body.'options'.Add('$skipToken', $Content.'$skipToken')
}
$Response = Invoke-AzRestMethod -Method 'Post' -Path '/providers/Microsoft.ResourceGraph/resources?api-version=2022-10-01' -Payload (
ConvertTo-Json -Depth 2 -Compress -InputObject $Body
)
if ($Response.'StatusCode' -ne 200) {
Throw ('That failed with HTTP status code {0}.' -f $Response.'StatusCode')
}
$Content = ConvertFrom-Json -InputObject $Response.'content' -AsHashtable
$Content.'data'.ForEach{$Resources.Add($_)}
Write-Output -InputObject ('Currently at {0}' -f $Resources.'Count'.ToString())
} until ([string]::IsNullOrEmpty($Content.'$skipToken'))
# Output total of resources
Write-Output -InputObject ('Found a total of {0} resources.' -f $Resources.'Count'.ToString()) Thanks to EnterprisePolicyAsCode for inspiration. 😊 |
Search-AzGraph
in Az.Accounts
v3.0.4 does not manage to parse the outputSearch-AzGraph
in Az.ResourceGraph
v1.0.0 does not manage to parse the output
Another workaround is to exclude properties you're not interested in, hopefully you'll only end up with parsable output. Example line: | project id, name, type, location, resourceGroup, subscriptionId, tags Full example query: resources
| where type in~ ("Microsoft.Automation/automationAccounts","Microsoft.DataFactory/factories","Microsoft.DocumentDB/databaseAccounts","Microsoft.Logic/workflows","Microsoft.KeyVault/vaults","Microsoft.Network/bastionHosts","Microsoft.Network/networkSecurityGroups","Microsoft.Network/publicIPAddresses","Microsoft.Network/virtualNetworkGateways","Microsoft.ServiceBus/namespaces")
| project id, name, type, location, resourceGroup, subscriptionId, tags
// Add tags from resource group
| join kind = leftouter (
resourcecontainers
| where type == "microsoft.resources/subscriptions/resourcegroups"
| project rgName = name, rgSubscriptionId = subscriptionId, rgTags = tags
) on $left.resourceGroup == $right.rgName and $left.subscriptionId == $right.rgSubscriptionId
| project-away rgName, rgSubscriptionId
// Add tags from subscription
| join kind = leftouter (
resourcecontainers
| where type == "microsoft.resources/subscriptions"
| project subName = name, subId = subscriptionId, subTags = tags
) on $left.subscriptionId == $right.subId
| project-away subId |
Let me loop in ResourceGraph team to understand if |
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @venu-l. |
1 similar comment
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @venu-l. |
Description
Following query works in Azure Resource Graph Explorer in the Azure portal ( https://portal.azure.com/#view/HubsExtension/ArgQueryBlade ), and if running
Search-AzGraph
with-Debug
I can see that the API returns data.But
Search-AzGraph
fails with error:Issue script & Debug output
Environment data
Module versions
Error output
The text was updated successfully, but these errors were encountered: