Search code examples
powershellazurepowershell-4.0azure-automation

Powershell to get private IP of specific VM


I am trying to get private IP of specific VM's. I have this code which is working

$vms = get-azurermvm -ResourceGroupName abc

$nics = get-azurermnetworkinterface -ResourceGroupName abc| 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
    Write-Output "$($vm.Name) : $prv"
}

I have VM with name es-client-node1, es-client-node2, es-master-node1, es-data-node1 & es-data-node1. I want to get IP address of just client node or VM's name matches with es-client-node*, similarly for datanode & master node into different variable

any idea how to do this in powershell ?


Solution

  • For getting private IP using PowerShell you can use this command:

    $IP = (Get-AzureRmNetworkInterface -Name $VMName -ResourceGroupName $RGName).IpConfigurations.PrivateIpAddress