how to retrieve private ip addresses and names of VMs running inside the virtual machine scale sets in azure

Daniyal Raza 91 Reputation points
2023-06-07T14:44:56.84+00:00

i want to fetch the list of private ip addresses and names of VMs running inside the VMSS. the total count is around 400.

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,178 questions
Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
352 questions
{count} votes

Accepted answer
  1. Grmacjon-MSFT 16,191 Reputation points
    2023-06-19T03:51:25.4133333+00:00

    Hi @Daniyal Raza ,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue:

    You wanted to know to retrieve a list of 400 private IP addresses and names of VMs running inside the virtual machine scale sets in Azure

    Solution:

    You found a powershell script that helped you accomplish your scenario:

    $resourceGroupName = "your_rg_name"
    $vmssName = "your_vmss_name"
    
    $nics = Get-AzNetworkInterface -ResourceGroupName $resourceGroupName -VirtualMachineScaleSetName $vmssName
    
    $vmIpAddresses = $nics | ForEach-Object {
        $vmResourceId = $_.VirtualMachine.Id
        $vmId = ($vmResourceId -split '/')[-1]
        $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmId
        $vmName = $vm.Name
        $privateIpAddress = $_.IpConfigurations[0].PrivateIpAddress
    
        [PSCustomObject]@{
            Name = "${vmssName}_${vmId}"
            PrivateIP = $privateIpAddress
        }
    }
    
    $vmIpAddresses
    
    

    Thanks again for sharing your solution.

    -Grace

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Daniyal Raza 91 Reputation points
    2023-06-07T16:23:50.5233333+00:00
    $resourceGroupName = "your_rg_name"
    $vmssName = "your_vmss_name"
    
    $nics = Get-AzNetworkInterface -ResourceGroupName $resourceGroupName -VirtualMachineScaleSetName $vmssName
    
    $vmIpAddresses = $nics | ForEach-Object {
        $vmResourceId = $_.VirtualMachine.Id
        $vmId = ($vmResourceId -split '/')[-1]
        $vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmId
        $vmName = $vm.Name
        $privateIpAddress = $_.IpConfigurations[0].PrivateIpAddress
    
        [PSCustomObject]@{
            Name = "${vmssName}_${vmId}"
            PrivateIP = $privateIpAddress
        }
    }
    
    $vmIpAddresses