w0lverine
w0lverine

Reputation: 39

AzureRmVM - Obtaining VMs Public IP addresses

I am trying to obtain IP addresses for all my virtual machines within my azure subscription. While I get the IP addresses of all my NICs, I can't seem to get them for my VMs. I am using AzureRm and powershell to obtain this information.

How do I obtain IP addresses of all my VMs within Azure using AzureRm?

Upvotes: 0

Views: 823

Answers (4)

D. Kadow
D. Kadow

Reputation: 1

Riffing on Charles Xu's excellent answer....

Converted to use the new 'Az' powershell module

foreach ($vm in $vms)
{
    $vmName = $vm.Name
   $resgrpName = $vm.ResourceGroupName
    $nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
    if ( (Get-AzNetworkInterface -ResourceGroupName $resgrpName -Name $nic).IpConfigurations.PublicIpAddress -eq $null )
    {
        continue
    }
    $publicIpName =  (Get-AzNetworkInterface -ResourceGroupName $resgrpName -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
    $publicIpAddress = (Get-AzPublicIpAddress -ResourceGroupName $resgrpName -Name $publicIpName).IpAddress
    Write-Output $vmName $publicIpAddress
}

It's wicked-slow, but does the job!

Upvotes: 0

Charles Xu
Charles Xu

Reputation: 31414

You can use Azure PowerShell to get all the VM Public IPs, but there is something you should pay attention to.

The VM can be associated with more than one network interface and each network interface can also associate with a public Ip.

I assume that your VMs in the same resource group and each VM just have one network interface. Then the PowerShell script will like this:

$vms = Get-AzureRmVM -ResourceGroupName yourRGName
foreach ($vm in $vms)
{
    $vmName = $vm.Name
    $nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
    if ( (Get-AzureRmNetworkInterface -ResourceGroupName yourRGName -Name $nic).IpConfigurations.PublicIpAddress -eq $null )
    {
        continue
    }
    $publicIpName =  (Get-AzureRmNetworkInterface -ResourceGroupName yourRGName -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
    $publicIpAddress = (Get-AzureRmPublicIpAddress -ResourceGroupName yourRGName -Name $publicIpName).IpAddress
    Write-Output $vmName $publicIpAddress
}

The result will like this:

enter image description here

If your VMs in the different resource groups, you can first get the VMs information and then everything is on the way. If there just exist your VMs in subscription, you can get all the VMs with the PowerShell command Get-azureRMVM when you log in the subscription. But if they're not just your VMs in the subscription, I think you'd better get all your VMs through resource groups. Hope this will help you.

Upvotes: 1

CHEEKATLAPRADEEP
CHEEKATLAPRADEEP

Reputation: 12768

You may refer the following steps to get all the public IP addresses in a Azure Subscription.

Gets all the public IP addresses in a subscription.

GET https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Network/publicIPAddresses?api-version=2018-08-01

Click on Try it

enter image description here

=> Sign in => Pick an Azure account => Select subscription Id => Run

enter image description here

Check out the Public IP addresses in the subscription:

Note: Only running VMs will have public IP address.

enter image description here

For more details, refer "Public IP Address - List All".

Hope this helps.

Upvotes: 0

Hannel
Hannel

Reputation: 1706

Because IP addresses in ARM are assigned to NICs not VM. You will have write a script to look at NICs assigned to VMs and look for IPs on those NICs.

Upvotes: 0

Related Questions