Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created July 7, 2021 13:31
Show Gist options
  • Save JohnLBevan/4e418230445002f339f4da5764004796 to your computer and use it in GitHub Desktop.
Save JohnLBevan/4e418230445002f339f4da5764004796 to your computer and use it in GitHub Desktop.
Query to list all of an application security group (ASG)'s members (VMs). Thanks to Clive for the graph query. https://feedback.azure.com/forums/217313-networking/suggestions/35655277-show-membership-of-application-security-groups
# https://docs.microsoft.com/en-us/azure/governance/resource-graph/first-query-powershell#run-your-first-resource-graph-query
# Install-Module -Name Az.ResourceGraph # - installs the module; no need to run if you've previously installed this.
# Login-AzAccount # brings up a web browser to log you in to Azure (interactive) so PS can run under your credentials
# Set-AzContext -SubscriptionId '0000000-1111-etc' # use this to target the subscription your resources live in; amending the subscrcription id as needed
(Search-AzGraph -Query @'
Resources
| where type =~ 'Microsoft.Compute/virtualMachines'
| project name, nics = properties.networkProfile.networkInterfaces
| mvexpand nics
| extend nicid = split(nics.id,'/')
| project name, nicname = tostring(nicid[(-1)])
| join (
Resources
| where type =~ 'Microsoft.Network/networkInterfaces'
| project name, asg = properties.ipConfigurations
| mvexpand asg
| mvexpand asg.properties.applicationSecurityGroups
| extend asgid = split(asg_properties_applicationSecurityGroups.id,'/')
| project nicname = name, asgname = tostring(asgid[(-1)])
) on nicname
| project vmname = name, asgname
| where isnotempty(asgname)
| order by vmname desc
'@).Data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment