3

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?

2
  • i dont think there's an easier way
    – 4c74356b41
    Nov 6, 2018 at 16:11
  • Does the answer solve your problem? Or there is something wrong with the solution? I also can provide more help if you need.
    – Charles Xu
    Nov 12, 2018 at 6:02

8 Answers 8

3

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

1
  • 1
    actually, the grep argument is 'ipAddress' Jan 7, 2022 at 0:22
2

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
0

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
    }
}
0

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
0

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
0

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).*" }
0

#Get the VM object $vm = Get-AzVM -Name $vmName -Status

#Get name of network adapter object attached to VM $NetworkInterfaceName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/") | Select -Last 1

#Get network adaptor object attached to VM $NetworkInterfaceObject = Get-AzNetworkInterface -Name $NetworkInterfaceName

#Get public IP Address object name attached to network adaptor object $ipObjectName = $NetworkInterfaceObject.IpConfigurations.PublicIpAddress.Id.Split("/") | Select -Last 1

#get publivc IP Address attached to public IP Address object
$ipObject = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name $ipObjectName

Write-Output $ipObject.IpAddress

2
  • Hello, please see meta.stackoverflow.com/editing-help Thanks!
    – Eric Aya
    Oct 29, 2022 at 11:24
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 1, 2022 at 16:00
0

Yet another method from within a Linux VM.

First, install the Azure command-line tools in the VM, see Azure docs

Second, execute the following in a shell on the VM:

az network public-ip list  --query  "[?dnsSettings.domainNameLabel=='MY_VM']"

where MY_VM is (hopefully) the host name of your VM. The command returns a multiline JSON string which is a list. Example is shown below:

[
  {
    "dnsSettings": {
      "domainNameLabel": "MY_VM",
      "fqdn": "my_vm.westeurope.cloudapp.azure.com"
    },
    "etag": "W/\"some_uuid...\"",
    ...
    "ipAddress": "AAA.BBB.CCC.DDD",

  },
  ...
]

How to parse the FQDN and the public IP out of this is left as an exercise to the reader :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.