Get current IP addresses associated with an Azure ARM VM's set of NICs via Powershell Get current IP addresses associated with an Azure ARM VM's set of NICs via Powershell azure azure

Get current IP addresses associated with an Azure ARM VM's set of NICs via Powershell


I use this code to get all my ARM VMs, their private IP address and allocation method, it works across resource groups.

$vms = get-azurermvm$nics = get-azurermnetworkinterface | where VirtualMachine -NE $null #skip Nics with no VMforeach($nic in $nics){    $vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id    $prv =  $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress    $alloc =  $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAllocationMethod    Write-Output "$($vm.Name) : $prv , $alloc"}

Sample Output:
proddc : 10.0.0.4 , Static
stagedc : 10.1.0.4 , Static


Below is the script I used to get the Private and Public IP for an Azure ARM VM. If a VM has more than one NIC or IpConfig it would probably need to use a loop.

$rg = Get-AzureRmResourceGroup -Name "MyResourceGroup01"$vm = Get-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name "MyVM01"$nic = Get-AzureRmNetworkInterface -ResourceGroupName $rg.ResourceGroupName -Name $(Split-Path -Leaf $VM.NetworkProfile.NetworkInterfaces[0].Id)$nic | Get-AzureRmNetworkInterfaceIpConfig | Select-Object Name,PrivateIpAddress,@{'label'='PublicIpAddress';Expression={Set-Variable -name pip -scope Global -value $(Split-Path -leaf $_.PublicIpAddress.Id);$pip}}(Get-AzureRmPublicIpAddress -ResourceGroupName $rg.ResourceGroupName -Name $pip).IpAddress#Output:    Name      PrivateIpAddress PublicIpAddress----      ---------------- ---------------ipconfig1 10.0.0.10        MyVM01-pip40.80.217.1


For those that are looking for a solution that works across multiple subscriptions in a tenant, here's a script that loops through each subscription and reports on each private IP, NIC, VM, Resource Group and associated subscription. The output is in object format and is exported to a CSV file.

<#    .SYNOPSIS        Returns IP addresses and associated network interfaces and virtual machines across all Azure subscriptions the        user has access to.    .DESCRIPTION        This script returns all private IP addresses, the IP configuration resources they are associated with, the network interfaces and virtual        machines across all subscriptions. This script requires:        1. The Azure module to be installed (https://docs.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-2.8.0)        2. The user to be logged in to an Azure account using Connect-AzAccount / Connect-AzureRmAccount        3. The user must have subscription wide read permissions assigned for each subscription being queried    .PARAMETER FileName        Optional. Specify the file name and path for a CSV export.    .EXAMPLE        Get-IpAddressAllocation.ps1 -FileName .\AzureIpAddressReport.csv#><#    .AUTHOR        Michael Wheatfill    .LICENSEURI        https://github.com/mwheatfill/mwheatfill.github.io/blob/master/LICENSE.txt#>#region Parameters[CmdletBinding()]param (    [Parameter(Mandatory=$true)]    [ValidateNotNullOrEmpty()]    [String]    $FileName)#endregion Parameters#region InitializationsSet-StrictMode -Version Latest$ErrorActionPreference = "Stop"#endregion Initializations#region Functionsfunction Get-IpAddresses {    param ()    $networkInterfaces = Get-AzNetworkInterface | Where-Object {$_.VirtualMachine -ne $null}    $virtualMachines = Get-AzVM    $results = @()    foreach($interface in $networkInterfaces) {        $ipConfigurations = $interface.IpConfigurations        foreach($ipConfig in $ipConfigurations) {            $vm = $virtualMachines | Where-Object {$_.Id -eq $interface.VirtualMachine.Id}            $ipDetails = [pscustomobject]@{                PrivateIpAddress = $ipConfig.PrivateIpAddress                VMName = $vm.Name                NetworkInterface = $interface.Name                IpConfigName = $ipConfig.Name                Primary = $ipConfig.Primary                ResourceGroup = $vm.ResourceGroupName                Subscription = $subscription.Name            }            $results += $ipDetails        }    }    return $results}#endregion Functions#region Main$subscriptions = Get-AzSubscription | Select-Object$ipAddressesInAllSubscriptions = @()$progressCount = 0foreach ($subscription in $subscriptions) {    $progressCount++    $progressComplete = ($progressCount / $subscriptions.count * 100)    $progressMessage = "Gathering IP address informtion for subscription $progressCount of $($subscriptions.Count)"    Write-Progress -Activity $progressMessage -Status ($subscription.Name) -PercentComplete $progressComplete    $subscription | Select-AzSubscription > $null    $ipAddressesInSubscription = Get-IpAddresses -SubscriptionObject $subscription    $ipAddressesInAllSubscriptions += $ipAddressesInSubscription}$ipAddressesInAllSubscriptions | Sort-Object -Property Subscription, VMName, NetworkInterface, IpConfigName, Primary | Format-Table$ipAddressesInAllSubscriptions | Export-Csv -Path $FileName -NoTypeInformation#endregion Main