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

Earlyflash picture Earlyflash · Jun 27, 2016 · Viewed 16.9k times · Source

I'm trying to write some Powershell to get a list of Azure ARM vms (Not classic) and the currently associated IP addresses for their NICs.

In classic, this was part of the VM object, but in ARM it's a seperate object, and I'm struggling to get the Powershell to work in the way I want.

I've got the following code segment:

$nic = (((Get-AzureRmVM).NetworkProfile).NetworkInterfaces).Id
ForEach ($i in $nic) {
  $nicname = $i.substring($i.LastIndexOf("/")+1)
  Get-AzureRmNetworkInterface -Name $nicname -ResourceGroupName RGTEST | Get-AzureRmNetworkInterfaceIpConfig | select-object  PrivateIpAddress,PrivateIpAllocationMethod
}

Which works, but only for VMs in the specified resource group 'RGTEST'.

It seems that Get-AzureRmNetworkInterface can only work when passed in the NIC Name and the ResourceGroupName, but I can't seem to get the RGname from the VM to be passed in.

Probably really easy, but I'm struggling with it!

Answer

Francois picture Francois · Jun 27, 2016

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 VM

foreach($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