Skip to content

Commit

Permalink
Added Azure Unused Resource script
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthebrit committed Apr 28, 2020
1 parent 887c7a2 commit 61f56f9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions AzureCostManagement/Get-AzureUnusedResources.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
$subs=Get-AzSubscription

$commandsToDelete = @()

foreach($sub in $subs)
{
Select-AzSubscription -Subscription $sub | Out-Null
Write-Output "`nChecking subscription : $($sub.Name) ($($sub.id))"

#Check for unconnected managed disks
$disks = Get-AzDisk
foreach($disk in $disks)
{
if($disk.ManagedBy -eq $null)
{
Write-Output " Disk $($disk.name) not connected to VM"
$commandsToDelete += "Remove-AzDisk -ResourceGroupName '$($disk.ResourceGroupName)' -DiskName '$($disk.name)'"
}
}
$pubIPs = Get-AzPublicIpAddress
$NATGWs = Get-AzNatGateway
foreach($pubIP in $pubIPs)
{
if($pubIP.IpConfiguration -eq $null)
{
Write-Output " Public IP $($pubIP.name) not connected to VM/LB, but could be used by different resource such as NAT Gateway"
$foundWithResource = $false
foreach($NATGW in $NATGWs)
{
foreach($GWPubIP in $NATGW.PublicIpAddresses)
{
if($GWPubIP.Id -eq $PubIP.id)
{
Write-Output " IP $($pubIP.name) used by NAT Gateway $($NATGW.Name)"
$foundWithResource = $true
}
}
}
if(!$foundWithResource)
{
$commandsToDelete += "Remove-AzPublicIPAddress -ResourceGroupName '$($pubIP.ResourceGroupName)' -Name '$($pubIP.name)'"

}
}
}
}

Write-Output "`n`nAnaysis Complete. To remove identified resources execute the commands below:`n"
Write-Output $commandsToDelete

0 comments on commit 61f56f9

Please sign in to comment.