getting image from azure compute gallery in c sharp

Dharshan M 0 Reputation points

i need to get the image from the azure compute gallery to create a virtual machine.

so help me with to get the image from the azure compute gallery.

   public void DOCreateVMFromImage()
            string captureimage = "Image1";
            string vmName = "Test-3";
            string resourceGrp = "TESTVMGROUP";
            string resource = "TESTVMGROUP"; 
            string adminUsername = "Student1";
            string adminPassword = "student1Excel";
           // var location = Region.USEast;
            var vNetName = "VNET-Fluent";
            var vNetAddress = "";
            var subnetName = "Subnet-Fluent";
            var subnetAddress = "";
            var nicName = "NIC-CVM";
            var publicIPName = "TestpublicIp";
            var nsgName = "NSG-Fluent";
            var sharedImageGalleryName = "NewImages";  // Replace with your actual gallery name
            var sharedImageDefinitionName = "Image1";  // Replace with your actual image definition name
            var sharedImageVersion = "0.0.1";

            var credentials = SdkContext.AzureCredentialsFactory

            var azure = Azure.Authenticate(credentials).WithDefaultSubscription();

            // Get the captured image
            var capturedImage = azure.VirtualMachineImages.GetImage(resourceGrp, sharedImageGalleryName, sharedImageDefinitionName, sharedImageVersion);

            if (capturedImage != null)
             //  var resourceGroup = azure.ResourceGroups.Define(resource)

                Console.WriteLine($"Creating virtual network {vNetName} ...");
                var network = azure.Networks.Define(vNetName)
                    .WithSubnet(subnetName, subnetAddress)

                Console.WriteLine($"Creating public IP {publicIPName} ...");
                var publicIP = azure.PublicIPAddresses.Define(publicIPName)
                //You need a network security group for controlling the access to the VM
                Console.WriteLine($"Creating Network Security Group {nsgName} ...");
                var nsg = azure.NetworkSecurityGroups.Define(nsgName)
                //You need a security rule for allowing the
                Console.WriteLine($"Creating a Security Rule for allowing the remote");

                Console.WriteLine($"Creating network interface {nicName} ...");
                var nic = azure.NetworkInterfaces.Define(nicName)

                Console.WriteLine($"Creating a new VM using {captureimage}...");

                var newVM = azure.VirtualMachines.Define(vmName)
                ///CheckVMStatus(azure, newVM.Id);
               ///ShutDownVM(azure, newVM.Id);

                Console.WriteLine("Successfully created a new VM: {0}!", vmName);
                Console.WriteLine("Press any key to exit...");
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.
10,629 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 19,706 Reputation points

    The code snippet you've provided includes most of what you need to accomplish this task. I will try to explain it step by step :

    The SdkContext.AzureCredentialsFactory.FromFile will load theAzure credentials from a configuration file for authenticating your application with Azure services.

    The Azure.Authenticate(credentials).WithDefaultSubscription() to authenticate and select the default subscription.

    Then to retrieve the image from the Azure Compute Gallery, you used these variables sharedImageGalleryName, sharedImageDefinitionName, and sharedImageVersion where azure.VirtualMachineImages.GetImage is used to retrieve the image using them.

    Then you defined and created a virtual network azure.Networks.Define, a public IP address azure.PublicIPAddresses.Define, and a network security group azure.NetworkSecurityGroups.Define.

    The azure.NetworkInterfaces.Define is to create a network interface for the VM.

    Then to create the VM, you used the azure.VirtualMachines.Define and specifying the VM's region, resource group, network interface, image, admin credentials, computer name, and size.

    If you were looking to understand the code, here is an explanation.

    0 comments No comments