Private/New-AzureUtilsDuplicateResourceQuery.ps1
|
function New-AzureUtilsDuplicateResourceQuery { <# .SYNOPSIS Builds the Resource Graph (KQL) query for likely duplicate resources. .DESCRIPTION Pure, side-effect-free. Groups resources by a case-insensitive name + type key, keeps the keys that occur more than once, then joins back so every member of a duplicated group is returned (with the group size in 'cnt'). Same name and type across different resource groups or subscriptions is often legitimate - this is a heuristic to surface accidental duplication. .OUTPUTS System.String (the KQL query) #> [CmdletBinding()] [OutputType([string])] param() return @' resources | extend dupKey = strcat(tolower(name), '|', tolower(type)) | join kind=inner ( resources | extend dupKey = strcat(tolower(name), '|', tolower(type)) | summarize cnt = count() by dupKey | where cnt > 1 ) on dupKey | extend reason = strcat('Duplicate name and type (', tostring(cnt), ' occurrences)') | project id, name, type, resourceGroup, location, subscriptionId, tags, cnt, reason | order by name asc '@ } |