In C# how to get the VMSS instance private IP

pipi 25 Reputation points
2025-12-02T07:05:59.1266667+00:00

In C# how can i get the Private IP address from the network interface of VMSS instance?

Resource ID: /subscriptions/<subscriptionid>/resourceGroups/<resourcegroup>/providers/Microsoft.Compute/virtualMachineScaleSets/<vmss>/virtualMachines/<instanceid>/networkInterfaces/


Moved from: Community Center | Not monitored

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ankit Yadav 6,270 Reputation points Microsoft External Staff Moderator
    2025-12-02T07:24:21.11+00:00

    Hey @pipi ,

    It seems you're trying to retrieve the private IP address of a specific instance within a Virtual Machine Scale Set (VMSS) using C#. Given your resource ID structure, here’s how you can achieve this:

    Steps to Get Private IP Address in C#

    1. Retrieve the Network Interface: You should start by obtaining the network interface associated with the VMSS instance. You can use the Azure SDK for .NET, specifically the VirtualMachineScaleSetVMs API to get the instance details.
    2. Get IP Configurations: After you have the network interface details, you can access the IP configurations, which will include the private IP address.

    Here’s some example C# code demonstrating this:

    using Microsoft.Azure.Management.Fluent;
    using Microsoft.Azure.Management.Fluent.Models;
    
    // Initialize Azure credentials and the management client
    
    var azure = Azure.Configure()
        .Authenticate(/* your credentials */)
        .WithDefaultSubscription();
    
    // Specify your resource group, VMSS name, and instance ID
    
    string resourceGroupName = "<resourcegroup>";
    string vmssName = "<vmss>";
    int instanceId = <instanceid>; // instance ID as an integer
    // Get the VMSS instance
    
    var vmssInstance = azure.VirtualMachineScaleSets
        .GetByResourceGroup(resourceGroupName, vmssName)
        .GetVirtualMachines()
        .GetById(instanceId);
    
    // Get the network interfaces
    
    var networkInterfaces = vmssInstance.NetworkProfile.NetworkInterfaces;
    // Loop through network interfaces to find the private IP
    
    foreach (var nic in networkInterfaces)
    {
        var ipConfigList = nic.IpConfigurations;
        foreach (var ipConfig in ipConfigList)
        {
            var privateIpAddress = ipConfig.PrivateIPAddress;
            // Output private IP Address
            Console.WriteLine($"Private IP Address: {privateIpAddress}");
        }
    }
    

    Troubleshooting

    If you're not getting the expected results using VirtualMachineScaleSetNetworkResource, ensure that:

    • You have the right permissions to access the network interface and its configurations.
    • You're referencing the correct instance ID and network interface configuration.

    Additional Resources

    If you're still encountering issues, let me know, and we can explore the options


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.