2

I'm trying to list unused, unattached, and unassociated resources inside Azure using Azure CLI.

So far, I've got unmanaged disks using the command:

unmanagedDiskNames=$(az disk list -g $rgName --query "[?(managedBy==null)].name" -o tsv)

I've got unattached Network interfaces using the command:

unattachedNicsIds=$(az network nic list -g $rgName --query "[?(virtualMachine==null)].id" -o tsv)

I'm having issued listing Public IPs and Network security groups. Tried to get public IPs using the command (it not worked):

unassociated_publicIPs=$(az network public-ip list -g "Technology-RG" --query "[?(IpConfiguration==null)].id" -o tsv)

Can you help me get unassociated public IPs and NSGs? Thank you.

Yuval Podoksik
  • 508
  • 2
  • 7
  • 23
  • Thanks for adding this here. I'm working on deleting unused resources and I've used this code to make a simple bash script to delete anything unused. – Alex Kaszynski Nov 01 '20 at 17:34

2 Answers2

2

It is a case sensitive issue, your "IpConfiguration==null" has uppercase "I", replace it to lowercase "ipConfiguration==null" and then you get the right result.

Taguada
  • 428
  • 1
  • 5
  • 8
  • Thank you. Do you have any suggestions about NSGs? Where can I list all available queries of resources when using the --query inside the CLI? – Yuval Podoksik Apr 28 '20 at 18:21
  • In this case NSG is associated to either SUBNET or NETWORKINTERFACES, so you can filter by ""networkInterfaces": null and""subnets": null". You should try something like that to get starts (az network nsg list -g site1 --query "[?networkInterfaces == null && subnets == null].id" -o tsv) – Taguada Apr 28 '20 at 20:38
1

Solved, the command for listing unassociated public IPs using Azure CLI is:

az network public-ip list -g $rgName --query "[?(ipConfiguration==null)].id" -o tsv

The command for listing unassociated network security groups IPs using Azure CLI is:

az network nsg list -g $rgName --query "[?(subnets==null) && (networkInterfaces==null)].id" -o tsv
Yuval Podoksik
  • 508
  • 2
  • 7
  • 23