Create Azure Virtual Machine using C# (Single Subnet)

Nuget:

Install-Package Microsoft.Azure.Management.Fluent

Using Statement to use :

using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;



Main Method:

 static void Main(string[] args)
        {
            //local resources
            var groupName = "dgkResourceGroup";
            var vmName = "dgkVM";
            var location = Region.USCentral;

            Console.WriteLine("Init....");
            printImageNames();
    //        var credentials = SdkContext.AzureCredentialsFactory
    //.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));

//            var credentials = SdkContext.AzureCredentialsFactory
//.FromServicePrincipal("79832ed1-680b-4cbc-9b20-c63010500419", "krMN6X2PW36nkX3smxvkRVtEstvlDdV6a+WdB5mztnQ=", "77ab64d0-48c5-4723-9a64-eb43ae84e773", AzureEnvironment.AzureGlobalCloud);

//            Console.WriteLine("Authenticating...");
//            var azure = Azure
//                .Configure()
//                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
//                .Authenticate(credentials)
//                .WithDefaultSubscription();


//                Console.WriteLine("Creating resource group...");
//            var resourceGroup = azure.ResourceGroups.Define(groupName)
//                .WithRegion(location)
//                .Create();

//            Console.WriteLine("Creating availability set...");
//            var availabilitySet = azure.AvailabilitySets.Define("dgkAVSet")
//                .WithRegion(location)
//                .WithExistingResourceGroup(groupName)
//                .WithSku(AvailabilitySetSkuTypes.Managed)
//                .Create();

//            Console.WriteLine("Creating public IP address...");
//            var publicIPAddress = azure.PublicIPAddresses.Define("dgkPublicIP")
//                .WithRegion(location)
//                .WithExistingResourceGroup(groupName)
//                .WithDynamicIP()
//                .Create();

//            Console.WriteLine("Creating virtual network...");
//            var network = azure.Networks.Define("dgkVNet")
//                .WithRegion(location)
//                .WithExistingResourceGroup(groupName)
//                .WithAddressSpace("10.0.0.0/16")
//                .WithSubnet("dgkSubnet", "10.0.0.0/24")
//                .Create();

//            Console.WriteLine("Creating network interface...");
//            var networkInterface = azure.NetworkInterfaces.Define("dgkNIC")
//                .WithRegion(location)
//                .WithExistingResourceGroup(groupName)
//                .WithExistingPrimaryNetwork(network)
//                .WithSubnet("dgkSubnet")
//                .WithPrimaryPrivateIPAddressDynamic()
//                .WithExistingPrimaryPublicIPAddress(publicIPAddress)
//                .Create();

//            Console.WriteLine("Creating virtual machine...");
//            azure.VirtualMachines.Define(vmName)
//                .WithRegion(location)
//                .WithExistingResourceGroup(groupName)
//                .WithExistingPrimaryNetworkInterface(networkInterface)
//                .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
//                .WithAdminUsername("azureuser")
//                .WithAdminPassword("Azure12345678")
//                .WithComputerName(vmName)
//                .WithExistingAvailabilitySet(availabilitySet)
//                .WithSize(VirtualMachineSizeTypes.StandardDS1V2)
//                .Create();
            
//            #region Adding an Existing disk

//            //Console.WriteLine("Creating virtual Disk...");
//            //var managedDisk = azure.Disks.Define("dgkosdisk")
//            //            .WithRegion(location)
//            //            .WithExistingResourceGroup(groupName)
//            //            .WithWindowsFromVhd("https://mystorage.blob.core.windows.net/vhds/dgkosdisk.vhd")
//            //            .WithSizeInGB(128)
//            //            .WithSku(DiskSkuTypes.StandardLRS)
//            //            .Create();

//            //Console.WriteLine("Defining virtual machine...");
//            //azure.VirtualMachines.Define(vmName)
//            //    .WithRegion(location)
//            //    .WithExistingResourceGroup(groupName)
//            //    .WithExistingPrimaryNetworkInterface(networkInterface)
//            //    .WithSpecializedOSDisk(managedDisk, OperatingSystemTypes.Windows)
//            //    .WithExistingAvailabilitySet(availabilitySet)
//            //    .WithSize(VirtualMachineSizeTypes.StandardDS1)
//            //    .Create();

//            #endregion

//            var vm = azure.VirtualMachines.GetByResourceGroup(groupName, vmName);

//            Console.WriteLine("Details about VM");
//            Console.WriteLine("================================================================");

//            Console.WriteLine("Getting information about the virtual machine...");
//            Console.WriteLine("hardwareProfile");
//            Console.WriteLine("   vmSize: " + vm.Size);
//            Console.WriteLine("storageProfile");
//            Console.WriteLine("  imageReference");
//            Console.WriteLine("    publisher: " + vm.StorageProfile.ImageReference.Publisher);
//            Console.WriteLine("    offer: " + vm.StorageProfile.ImageReference.Offer);
//            Console.WriteLine("    sku: " + vm.StorageProfile.ImageReference.Sku);
//            Console.WriteLine("    version: " + vm.StorageProfile.ImageReference.Version);
//            Console.WriteLine("  osDisk");
//            Console.WriteLine("    osType: " + vm.StorageProfile.OsDisk.OsType);
//            Console.WriteLine("    name: " + vm.StorageProfile.OsDisk.Name);
//            Console.WriteLine("    createOption: " + vm.StorageProfile.OsDisk.CreateOption);
//            Console.WriteLine("    caching: " + vm.StorageProfile.OsDisk.Caching);
//            Console.WriteLine("osProfile");
//            Console.WriteLine("  computerName: " + vm.OSProfile.ComputerName);
//            Console.WriteLine("  adminUsername: " + vm.OSProfile.AdminUsername);
//            Console.WriteLine("  provisionVMAgent: " + vm.OSProfile.WindowsConfiguration.ProvisionVMAgent.Value);
//            Console.WriteLine("  enableAutomaticUpdates: " + vm.OSProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
//            Console.WriteLine("networkProfile");
//            foreach (string nicId in vm.NetworkInterfaceIds)
//            {
//                Console.WriteLine("  networkInterface id: " + nicId);
//            }
//            Console.WriteLine("vmAgent");
//            Console.WriteLine("  vmAgentVersion" + vm.InstanceView.VmAgent.VmAgentVersion);
//            Console.WriteLine("    statuses");
//            foreach (InstanceViewStatus stat in vm.InstanceView.VmAgent.Statuses)
//            {
//                Console.WriteLine("    code: " + stat.Code);
//                Console.WriteLine("    level: " + stat.Level);
//                Console.WriteLine("    displayStatus: " + stat.DisplayStatus);
//                Console.WriteLine("    message: " + stat.Message);
//                Console.WriteLine("    time: " + stat.Time);
//            }
//            Console.WriteLine("disks");
//            foreach (DiskInstanceView disk in vm.InstanceView.Disks)
//            {
//                Console.WriteLine("  name: " + disk.Name);
//                Console.WriteLine("  statuses");
//                foreach (InstanceViewStatus stat in disk.Statuses)
//                {
//                    Console.WriteLine("    code: " + stat.Code);
//                    Console.WriteLine("    level: " + stat.Level);
//                    Console.WriteLine("    displayStatus: " + stat.DisplayStatus);
//                    Console.WriteLine("    time: " + stat.Time);
//                }
//            }
//            Console.WriteLine("VM general status");
//            Console.WriteLine("  provisioningStatus: " + vm.ProvisioningState);
//            Console.WriteLine("  id: " + vm.Id);
//            Console.WriteLine("  name: " + vm.Name);
//            Console.WriteLine("  type: " + vm.Type);
//            Console.WriteLine("  location: " + vm.Region);
//            Console.WriteLine("VM instance status");
//            foreach (InstanceViewStatus stat in vm.InstanceView.Statuses)
//            {
//                Console.WriteLine("  code: " + stat.Code);
//                Console.WriteLine("  level: " + stat.Level);
//                Console.WriteLine("  displayStatus: " + stat.DisplayStatus);
//            }
//            Console.WriteLine("Press enter to continue...");
//            Console.ReadLine();



        }





public static void printImageNames()
        {
            var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal("79832ed1-680b-4cbc-9b20-c63010500419", "krMN6X2PW36nkX3smxvkRVtEstvlDdV6a+WdB5mztnQ=", "77ab64d0-48c5-4723-9a64-eb43ae84e773", AzureEnvironment.AzureGlobalCloud);

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

            var publishers = azure
                   .VirtualMachines
                   .Sizes
                   .ListByRegion(Region.USCentral);

            foreach (var publisher in publishers)
            {
                Console.WriteLine(publisher.Name);
                //if(StringComparer.OrdinalIgnoreCase.Equals(publisher.Name, "MicrosoftWindowsDesktop") || StringComparer.OrdinalIgnoreCase.Equals(publisher.Name, "MicrosoftWindowsServer"))
                //{
                //    foreach (var offer in publisher.Offers.List())
                //    {
                //        foreach (var sku in offer.Skus.List())
                //        {
                //            foreach (var image in sku.Images.List())
                //            {
                //                Console.WriteLine($"Image - {publisher.Name}/{sku.Name}/{image.Version}");

                //            }
                //        }
                //    }
                //}
                
            }
            Console.Read();

        }

Comments

Popular Posts