Update : updated script to support Azure Az powershell module.
Today, I am back with another inventory type powershell script. I recently wrote this to generate Azure Network Interface Card(NIC) details in an Azure Subscription.
I wrote this script to identify any orphan NIC with Public IP not attached to any VM . Though , it is not very expensive, we still need to delete those as it adds to the Azure consumption bill. You may refer to following Azure Cost Calculator for detailed pricing – https://azure.microsoft.com/en-us/pricing/details/ip-addresses/ .
Before start using this script, make sure, you have Azure Az Module installed and imported on the system. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0
Let me know if it helps you!
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\AzureNIC_Details.csv"
if(Test-Path $oFile){Remove-Item $oFile -Force}
"ResourceGroupName,NICName,AttachedVM,ProvisioningState,Location" | Out-File $oFile -Append -Encoding ASCII
Get-AzNetworkInterface | ForEach-Object{
$rgName = $nicName = $provState = $location = $managedBy = ""
$rgName = $_.ResourceGroupName
$nicName = $_.Name
$managedBy = if($_.VirtualMachine){$_.VirtualMachine.Id.split("/")[-1]}
$provState = $_.ProvisioningState
$location = $_.Location
"$rgName,$nicName,$managedBy,$provState,$location" | Out-File $oFile -Append -Encoding ASCII
}
Download above script and save it with a .ps1 file extension. Open powershell console. After that, login to your Azure account using – Login-AzAccount. It will prompt you to enter your Azure credentials.
After you login to Azure, run the powershell script saved in previous step.
You will receive a csv file output like below: