Easier way of retrieving an Azure VM's Public IP address

Viewed 5554

Using the name/resource group of a specific VM, I'm trying to get the VM's public IP address.

This code works but it seems unwieldy in comparison to other AzureRM cmdlets.

$VM = Get-AzureRmVM -ResourceGroupName MyResourceGroup -Name MyVMName
$NIC = $VM.NetworkProfile.NetworkInterfaces[0].Id -replace '.*\/'
$NI = Get-AzureRmNetworkInterface -Name $NIC -ResourceGroupName MyResourceGroup
$NIIC = Get-AzureRmNetworkInterfaceIpConfig -NetworkInterface $NI
$PIP = $NIIC.PublicIpAddress.Id -replace '.*\/'
$PIP = Get-AzureRmPublicIpAddress -Name $PIP -ResourceGroupName MyResourceGroup
$PIP.IpAddress

Is there a quicker/easier/shorter way of accessing this information?

6 Answers

As far as i know, Not Yet for PowerShell. But you can use Azure CLI

az vm list-ip-addresses -n <VMName> -g <ResourceGroup> | grep publicIpAddresses

enter image description here

Try the Azure CLI command:

az vm list-ip-addresses -g groupName -n vmName --query "[].virtualMachine.network.publicIpAddresses[*].ipAddress" -o tsv

Or the PowerShell command just filter with your vm name:

$ipAddress= (Get-AzureRmPublicIpAddress -ResourceGroupName groupName | Where-Object { $_.IpConfiguration.Id -like "*vmName*" }
)
$ipAddress.IpAddress

The way i got the value for my Linux VM's was using below code.

Get-AzureRmPublicIpAddress -ResourceGroupName <yourRG> -Name <yourVMName> | Select-Object {$_.IpAddress}

This will return something of this sort:

$_.IpAddress
------------
52.170.56.60

It's possible. This script will list all VMs PIP in your Azure cloud.

OLD

$VM_int = Get-AzureRmResource -ODataQuery "`$filter=resourcetype 'Microsoft.Compute/virtualMachines'"
foreach($int in $VM_int){
$vmName = $int.Name
$ipAddress= (Get-AzureRmPublicIpAddress -ResourceGroupName $int.ResourceGroupName | Where-Object { $_.IpConfiguration.Id -like "*$vmName*" })
$vmName + ' --- ' + $ipAddress.IpAddress
}

UPDATE

Unfortunately, Get-AzVM doesn't provide the Public IP address of VM, but we can scrape its Network Interface Name and make a wildcard search of it through all assigned Public IPs which NIC name are matched. It's not fast but will provide with correct results.

$array = @()
foreach ($vm in Get-AzVM) {
    $vmNicName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]
    $ipAddress = Get-AzPublicIpAddress | Where-Object {$_.IpConfiguration.Id -like "*$vmNicName*"}
    if ($null -ne $ipAddress) {
        $pipInput = New-Object psobject -Property @{
            VM       = $vm.Name
            PublicIP = $ipAddress.IpAddress
        }
        $array += $pipInput
    }
}

This outputs a bit of information however the public IP address is in there.

Get-AzPublicIpAddress -ResourceGroupName MyResourceGroup | Where-Object {$_.name -like "*MyVMName*" }

Or you can do this to just get the IP address:

Get-AzPublicIpAddress -ResourceGroupName MyResourceGroup | Where-Object {$_.name -like "*MyVMName*" } | Select-Object { $_.IpAddress }

Output is like:

$_.IpAddress 
--------------
13.255.162.33

You can also match the AzPublicIpAddress IpConfiguration.Id with the VM's NetworkInterfaces.Id:

Get-AzPublicIpAddress | ?{$_.IpConfiguration.Id -match "$((Get-AzVM -Name $computername).NetworkProfile.NetworkInterfaces.Id).*" }
Related