Reputation: 39
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
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
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:
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
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
=> Sign in => Pick an Azure account => Select subscription Id => Run
Check out the Public IP addresses in the subscription:
Note: Only running VMs will have public IP address.
For more details, refer "Public IP Address - List All".
Hope this helps.
Upvotes: 0
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