1

Im using the Microsoft.Azure.Compute.Fluent sdk to list all my VMs and it´s working fine, except i cant get the public ip address:

    IVirtualMachines _client = azure.VirtualMachines; 
    var list = await _client.ListAsync();

    foreach (var instance in list)
    {
        var name = instance.Name;
        var ip = instance.GetPrimaryPublicIPAddress().IPAddress;
        //ip = null here;
    }

Well I Tried other stuff but always getting the public ip as null.

How can I retrieve the Public IP correctly?

gog
  • 11,788
  • 23
  • 67
  • 129
  • Maybe [this](https://stackoverflow.com/questions/43474171/how-to-get-public-ip-of-azure-vm-using-azure-sdk/43503511#43503511) is helpful. – Charles Xu Dec 18 '18 at 02:25
  • 1
    Does the answer below work for you? If you have more question, please let me know. If it works for you, could you please mark it as answer? thanks. – Ivan Glasenberg Dec 31 '18 at 09:38

1 Answers1

0

Please correct me if I misunderstand you: I can't find the sdk Microsoft.Azure.Compute.Fluent(screenshot as below):

enter image description here

So I use this sdk Microsoft.Azure.Management.Fluent instead(The official doc also uses it). And the ip can be fetched at my side, code as blow:

using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using System;
using System.Threading.Tasks;

namespace myVMDotnetProject
{
    class Program
    {
        static void Main(string[] args)
        {
            GetVMInfo();

            Console.WriteLine("okok");
            Console.ReadLine();
        }


        static async Task  GetVMInfo()
        {
            var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION", EnvironmentVariableTarget.User));

            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

            IVirtualMachines _client = azure.VirtualMachines;
            var list = await _client.ListAsync();

            foreach (var instance in list)
            {
                var name = instance.Name;
                var ip = instance.GetPrimaryPublicIPAddress().IPAddress;
                Console.WriteLine("name: " + name + ", ip: " + ip);
            }
        }

    }
}

Test result as below:

enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60