Here are a few scripts that I have recently used to find Azure resources that are no longer needed and can be removed to cut down payments.
First you will need to connect to your Azure account using Powershell.
# PublicIpAddress $public_addresses = Get-AzPublicIpAddress; foreach ($item in $public_addresses) { if($item.IpConfiguration -eq $null) { $item.Name } #else #{ # Write-Host -ForegroundColor Yellow $item.Name #} }
# NetworkSecurityGroup $nsgs = Get-AzNetworkSecurityGroup foreach ($item in $nsgs) { $nic = Get-AzNetworkSecurityGroup -Name $item.name -ResourceGroupName $item.resourcegroupname; if((-not $nic.NetworkInterfaces) -and (-not $nic.Subnets) ) { Write-Host $nic.Name -ForegroundColor Yellow; } }
# NetworkInterface if(-not $nics) { $nics = Get-AzNetworkInterface #| where-object { $_.VirtualMachine -eq $null } #| Remove-AzVMNetworkInterface #Get-AzNetworkInterface | where-object { $_.VirtualMachine -eq $null } | ft name } foreach ($item in $nics) { if(-not ($item.VirtualMachine)) { Write-Host $item.Name; } }
# Managed Disks if (-not $disks) { $disks = Get-AzDisk; } $counter = 0; foreach ($item in $disks) { if($item.ManagedBy -eq $null) { $counter +=1; $name = $item.Name; Write-Host $counter $item.ResourceGroupName $item.Name $item.DiskSizeGB -ForegroundColor Yellow; } }
The following two tabs change content below.
Yaniv Etrogi is an SQL Server consultant. He loves SQL Server and he is passionate about Performance Tuning, Automation and Monitoring.
Latest posts by Yaniv Etrogi (see all)
- Monitor AlwaysOn Availabilty Groups - July 6, 2023
- SQL Server – The secret index syntax - February 8, 2023
- Use .net SqlClient with Powershell to access data - January 25, 2023
- Use Powershell to find unused resources in Azure - August 5, 2022