Skip to content

Instantly share code, notes, and snippets.

@negeric
Created March 28, 2018 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negeric/1b88bb46cee327320e056b4e134df266 to your computer and use it in GitHub Desktop.
Save negeric/1b88bb46cee327320e056b4e134df266 to your computer and use it in GitHub Desktop.
Displays and removes Azure Network Interfaces that are not associated with a VM
<#
.SYNOPSIS
Deletes Azure Network Interfaces not associated with a VM
.DESCRIPTION
Iterates through the network interfaces in an Azure subscription
And deletes the interfaces not associated with a VM
Will log to a file if LogFile parameter is passed
.PARAMETER Force
Do not ask permission for each interface
This will delete all interfaces that are not associated with a VM
.PARAMETER LogFile
Path to a log file. Will use basic text logging
.INPUTS
[switch]Force
[string]LogFile
#>
Param
(
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Position=1)]
[switch]$Force = $false,
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Position=2)]
[string]$LogFile = $null
)
function CheckLogPath() {
#Check if path to log file exists
if (![string]::IsNullOrEmpty($LogFile)) {
try {
[System.IO.Path]::GetDirectoryName($LogFile)
} catch {
$TryNewFile = Read-Host "Log directory does not exist, continue without a log file? (y/n)"
if ($TryNewFile.ToLower() -eq "y") {
$LogFile = $null
} else {
$LogFile = Read-Host "Enter a new log file: "
CheckLogPath
}
}
}
}
CheckLogPath
function Write-Log {
Param (
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true,Position=1,Mandatory=$true)]
[string]$LogLine
)
if (![string]::IsNullOrEmpty($LogFile)) {
$d = Get-Date
$Dt = $d.ToUniversalTime()
$Log = "$Dt - $LogLine"
$Log | Add-Content $LogFile
}
}
#Log if -Force flag is set
Write-Log -LogLine "Force Flag is set, will not prompt user for input"
#Log into your azure account
if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {
Write-Log -LogLine "Showing Azure Login Prompt"
$null = Login-AzureRmAccount
} else {
Write-Log -LogLine "User has already authenticated to Azure"
}
Write-Host "========Select a Subscription========"
$Subscriptions = Get-AzureRmSubscription
$i = 0
foreach ($Sub in $Subscriptions) {
Write-Host "$i -" $Sub.Name
$i++
}
do {
$SelectedSub = Read-Host "Select a Subscription"
} while ($SelectedSub -ge $i -or $SelectedSub -lt 0)
$Subscription = $Subscriptions[$SelectedSub]
Write-Host "Changing to subscription" $Subscription.Name -ForegroundColor Green
try {
$null = Select-AzureRmSubscription -SubscriptionId $Subscription.Id -ErrorAction Continue
$SubscriptionName = $Subscription.Name
Write-Log -LogLine "User selected subscription $SubscriptionName"
} catch {
Write-Host "Error selecting Subscription $SubscriptionName. Please ensure that you have access to this subscription and try again" -ForegroundColor Red
Write-Log -LogLine "Error Accessing subscription $SubscriptionName"
Write-Log -LogLine $_.Exception.Message
}
$OrphanedInterfaces = Get-AzureRmNetworkInterface | Where-Object { $_.VirtualMachine -eq $null }
if ($OrphanedInterfaces.length -le 0) {
Write-Host "There are no orphaned interfaces in this subscription"
Write-Log -LogLine "There are no orphaned interfaces in this subscription"
exit
}
do {
Write-Host "========Delete Interface========"
$InterfacesToDelete = @{}
$InterfacesToKeep = @{}
foreach ($Int in $OrphanedInterfaces) {
if ($Force) {
$InterfacesToDelete.add($Int.Name, $Int.ResourceGroupName)
} else {
$Delete = Read-Host $Int.Name " (y/n)"
if ($Delete.ToLower() -eq "y") {
$InterfacesToDelete.add($Int.Name, $Int.ResourceGroupName)
} else {
$InterfacesToKeep.add($Int.Name, $Int.ResourceGroupName)
}
}
}
foreach ($DelInt in $InterfacesToDelete.Keys) {
Write-Host "Deleting" $DelInt -ForegroundColor Red
}
foreach ($SaveInt in $InterfacesToKeep.Keys) {
Write-Host "Keeping" $SaveInt -ForegroundColor Green
}
$ConfirmDelete = Read-Host "Confirm delete (y/n)"
} while ($ConfirmDelete.ToLower() -ne "y" -and $ConfirmDelete.ToLower() -ne "n")
if ($ConfirmDelete.ToLower() -eq "y") {
Write-Host "========DELETING INTERFACES========";
foreach ($Del in $InterfacesToDelete.Keys) {
try {
$RG = $InterfacesToDelete[$Del]
$null = Remove-AzureRmNetworkInterface -Name "$Del" -ResourceGroupName "$RG" -Force -ErrorAction Continue
Write-Host "Deleted " $InterfacesToDelete[$Del]"/"$Del
Write-Log -LogLine "Deleted" $InterfacesToDelete[$Del]"/"$Del
} catch {
Write-Host "Failed to delete $Del" -ForegroundColor Red
Write-Log -LogLine "Failed to delete $Del"
Write-Log -LogLine $_.Exception.Message
}
}
} else {
Write-Host "User cancelled script. Nothing has been deleted"
Write-Log -LogLine "User cancelled script. Nothing has been deleted"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment