Search code examples
azureazure-sdk

What's the difference between ListByResourceGroup() and GetByResourceGroup() in azure sdk?


When working with azure sdk I see ListByResourceGroup(), GetByResourceGroup() return the same value. What is the difference between them? If I want to get all information of vm in my subcriptions, what function should I use?


Solution

  • T GetByResourceGroup(string resourceGroupName, string name)
    
    IEnumerable<T> ListByResourceGroup(string resourceGroupName)
    

    So, if you use GetByResourceGroup, you need to provide 2 parameters. One is the resource group name, the other is the name for the VM. And you will only get the target VM.

    However, ListByResourceGroup only reqiure for the resource group name. And you will be able to get all the VMs in the resource group.

    If you want to get all the VMs, you can use the following as a sample:

        static void Main(string[] args)
        {
            IAzure azure = Azure.Authenticate("path_to_auth_file").WithDefaultSubscription();
    
            var vms = azure.VirtualMachines.List();
            foreach( var vm in vms)
            {
                Console.WriteLine(vm.Name);
            }
        }
    

    I just simply output the VM's name, you can do what you want in the foreach.