Open In App

Microsoft Azure – Handling Orphaned NSGs

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, you will see that how to find the un-used NSGs, and these are also known as orphaned NSGs in Azure. The main objective or purpose of deleting the orphaned NSGs resources is to save the cost for customers. When you delete a VM in Azure, the NSGs resource will not be deleted automatically and they will be left as orphaned NSGs.

Implementation:

Step 1: Log in to Azure Portal.

Step 2: Go to Azure Resource Graph Explorer >> select the Resource Graph Explorer

Step 3: Select Scope >> Subscription >> Select your subscriptions (You can select single or multiple subscriptions of your choice at once)

Step 4: Paste the below queries in the query box and click on the Run query to get the list/report of orphaned NSGs.

  • KQL Resource Graph Query: To get the complete property details of orphaned NSGs using azure resource graph query and to get the count of orphaned NSGs.
Resources
| where type =~ 'microsoft.network/networksecuritygroups' 
and isnull(properties.networkInterfaces) 
and isnull(properties.subnets)

Output:

Example 1:

KQL Resource Graph Query: To project the details of only name, location, tags equals “Productions“, resourceGroup, subscriptionId and to get the count of orphaned NSGs.

Resources
| where type =~ 'microsoft.network/networksecuritygroups' 
and isnull(properties.networkInterfaces) 
and isnull(properties.subnets)
| where tags.Environment == "Production"
| project name, location, tags, resourceGroup, subscriptionId

Output:

Example 2:

KQL Resource Graph Query: To project the details of only name, location, tags not equals null, resourceGroup, subscriptionId and to get the count of orphaned NSGs.

Resources
| where type =~ 'microsoft.network/networksecuritygroups' 
and isnull(properties.networkInterfaces) 
and isnull(properties.subnets)
| where tags.Environment != ""
| project name, location, tags, resourceGroup, subscriptionId

Output:


Last Updated : 31 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads