Create Azure Virtual Machines using C# (Multiple Subnets)
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:
public void CreateVirtualMachine(string vmName, string vmSize,
string vmImage, string vmUserName,
string vmPassword, string resourceGrp,
string avSetName,
string ipSetName, string ipRange,
string subNetRange, string subnetName,
string vNetName, string nicName, string prodSubNetRange)
{
//resetting error providers
passwordError.SetError(txtPassword, string.Empty);
//Add Values to Dictionary
Dictionary<string, string> subnetRanges = new Dictionary<string, string>();
subnetRanges.Add(subnetName + "_Dev", subNetRange);
subnetRanges.Add(subnetName + "_Prod", prodSubNetRange);
try
{
var location = Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral;
Console.WriteLine("Creating resource group...");
var resourceGroup = azure.ResourceGroups.Define(resourceGrp)
.WithRegion(location)
.Create();
Console.WriteLine("Creating availability set...");
var availabilitySet = azure.AvailabilitySets.Define(avSetName)
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithSku(AvailabilitySetSkuTypes.Managed)
.Create();
Console.WriteLine("Creating Dev public IP address...");
var publicIPAddressDev = azure.PublicIPAddresses.Define(ipSetName + "_Dev")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithDynamicIP()
.Create();
Console.WriteLine("Creating Production public IP address...");
var publicIPAddressProd = azure.PublicIPAddresses.Define(ipSetName + "_Prod")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithDynamicIP()
.Create();
Console.WriteLine("Creating virtual network...");
var network = azure.Networks.Define(vNetName)
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithAddressSpace(ipRange)
.WithSubnets(subnetRanges)
.Create();
Console.WriteLine("Creating Development network interface...");
var networkInterfaceDev = azure.NetworkInterfaces.Define(nicName + "_Development")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName + "_Dev")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddressDev)
.Create();
Console.WriteLine("Creating Production network interface...");
var networkInterfaceProd = azure.NetworkInterfaces.Define(nicName + "_Production")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName + "_Prod")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddressProd)
.Create();
Console.WriteLine("Creating development virtual machine...");
azure.VirtualMachines.Define(vmName + "Dev")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetworkInterface(networkInterfaceDev)
.WithLatestWindowsImage(drpImage.Text.Split('/')[0], drpImage.Text.Split('/')[1], drpImage.Text.Split('/')[2])
.WithAdminUsername(vmUserName)
.WithAdminPassword(vmPassword)
.WithComputerName(vmName + "Dev")
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardDS1V2)
.Create();
Console.WriteLine("Creating production virtual machine...");
azure.VirtualMachines.Define(vmName + "Prod")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetworkInterface(networkInterfaceProd)
.WithLatestWindowsImage(drpImage.Text.Split('/')[0], drpImage.Text.Split('/')[1], drpImage.Text.Split('/')[2])
.WithAdminUsername(vmUserName)
.WithAdminPassword(vmPassword)
.WithComputerName(vmName + "Prod")
.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
// List Down Created Development VM
var devVm = azure.VirtualMachines.GetByResourceGroup(resourceGrp, vmName + "Dev");
Console.WriteLine(" ");
Console.WriteLine("================================================================");
Console.WriteLine("Details about Development VM");
Console.WriteLine("================================================================");
Console.WriteLine("Getting information about the virtual machine...");
Console.WriteLine("hardwareProfile");
Console.WriteLine(" vmSize: " + devVm.Size);
Console.WriteLine("storageProfile");
Console.WriteLine(" imageReference");
Console.WriteLine(" publisher: " + devVm.StorageProfile.ImageReference.Publisher);
Console.WriteLine(" offer: " + devVm.StorageProfile.ImageReference.Offer);
Console.WriteLine(" sku: " + devVm.StorageProfile.ImageReference.Sku);
Console.WriteLine(" version: " + devVm.StorageProfile.ImageReference.Version);
Console.WriteLine(" osDisk");
Console.WriteLine(" osType: " + devVm.StorageProfile.OsDisk.OsType);
Console.WriteLine(" name: " + devVm.StorageProfile.OsDisk.Name);
Console.WriteLine(" createOption: " + devVm.StorageProfile.OsDisk.CreateOption);
Console.WriteLine(" caching: " + devVm.StorageProfile.OsDisk.Caching);
Console.WriteLine("osProfile");
Console.WriteLine(" computerName: " + devVm.OSProfile.ComputerName);
Console.WriteLine(" adminUsername: " + devVm.OSProfile.AdminUsername);
Console.WriteLine(" provisionVMAgent: " + devVm.OSProfile.WindowsConfiguration.ProvisionVMAgent.Value);
Console.WriteLine(" enableAutomaticUpdates: " + devVm.OSProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
Console.WriteLine("networkProfile");
foreach (string nicId in devVm.NetworkInterfaceIds)
{
Console.WriteLine(" networkInterface id: " + nicId);
}
Console.WriteLine("vmAgent");
Console.WriteLine(" vmAgentVersion" + devVm.InstanceView.VmAgent.VmAgentVersion);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus stat in devVm.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 devVm.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: " + devVm.ProvisioningState);
Console.WriteLine(" id: " + devVm.Id);
Console.WriteLine(" name: " + devVm.Name);
Console.WriteLine(" type: " + devVm.Type);
Console.WriteLine(" location: " + devVm.Region);
Console.WriteLine("VM instance status");
foreach (InstanceViewStatus stat in devVm.InstanceView.Statuses)
{
Console.WriteLine(" code: " + stat.Code);
Console.WriteLine(" level: " + stat.Level);
Console.WriteLine(" displayStatus: " + stat.DisplayStatus);
}
// List Down Created Production VMs'
var ProdVm = azure.VirtualMachines.GetByResourceGroup(resourceGrp, vmName + "Prod");
Console.WriteLine(" ");
Console.WriteLine("================================================================");
Console.WriteLine("Details about Production VM");
Console.WriteLine("================================================================");
Console.WriteLine("Getting information about the virtual machine...");
Console.WriteLine("hardwareProfile");
Console.WriteLine(" vmSize: " + ProdVm.Size);
Console.WriteLine("storageProfile");
Console.WriteLine(" imageReference");
Console.WriteLine(" publisher: " + ProdVm.StorageProfile.ImageReference.Publisher);
Console.WriteLine(" offer: " + ProdVm.StorageProfile.ImageReference.Offer);
Console.WriteLine(" sku: " + ProdVm.StorageProfile.ImageReference.Sku);
Console.WriteLine(" version: " + ProdVm.StorageProfile.ImageReference.Version);
Console.WriteLine(" osDisk");
Console.WriteLine(" osType: " + ProdVm.StorageProfile.OsDisk.OsType);
Console.WriteLine(" name: " + ProdVm.StorageProfile.OsDisk.Name);
Console.WriteLine(" createOption: " + ProdVm.StorageProfile.OsDisk.CreateOption);
Console.WriteLine(" caching: " + ProdVm.StorageProfile.OsDisk.Caching);
Console.WriteLine("osProfile");
Console.WriteLine(" computerName: " + ProdVm.OSProfile.ComputerName);
Console.WriteLine(" adminUsername: " + ProdVm.OSProfile.AdminUsername);
Console.WriteLine(" provisionVMAgent: " + ProdVm.OSProfile.WindowsConfiguration.ProvisionVMAgent.Value);
Console.WriteLine(" enableAutomaticUpdates: " + ProdVm.OSProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
Console.WriteLine("networkProfile");
foreach (string nicId in ProdVm.NetworkInterfaceIds)
{
Console.WriteLine(" networkInterface id: " + nicId);
}
Console.WriteLine("vmAgent");
Console.WriteLine(" vmAgentVersion" + ProdVm.InstanceView.VmAgent.VmAgentVersion);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus stat in ProdVm.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 devVm.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: " + devVm.ProvisioningState);
Console.WriteLine(" id: " + ProdVm.Id);
Console.WriteLine(" name: " + ProdVm.Name);
Console.WriteLine(" type: " + ProdVm.Type);
Console.WriteLine(" location: " + ProdVm.Region);
Console.WriteLine("VM instance status");
foreach (InstanceViewStatus stat in ProdVm.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();
}
catch (Microsoft.Rest.Azure.CloudException cex)
{
Console.WriteLine(cex.Message);
passwordError.SetError(txtPassword, cex.Message);
}
}
public void PopulateMachineSizes()
{
//Populate Machine Sizes
Console.WriteLine("Loading Machine Sizes.....");
var machineSizes = azure
.VirtualMachines
.Sizes
.ListByRegion(Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral);
foreach (var machinesize in machineSizes)
{
drpSize.Items.Add(machinesize.Name);
}
Console.WriteLine("Machine sizes loaded");
}
public void PopulateImages()
{
Console.WriteLine("Loading OS Images.....");
var images = azure
.VirtualMachineImages
.Publishers
.ListByRegion(Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral);
foreach (var image in images)
{
if (StringComparer.OrdinalIgnoreCase.Equals(image.Name, "MicrosoftWindowsServer"))
{
foreach (var offer in image.Offers.List())
{
foreach (var sku in offer.Skus.List())
{
foreach (var img in sku.Images.List())
{
drpImage.Items.Add($"{image.Name }/{offer.Name}/{sku.Name}/{img.Version}");
}
}
}
}
}
Console.WriteLine("OS Images loaded");
}
public void Authenticate()
{
var appID = System.Configuration.ConfigurationManager.AppSettings["appId"];
var appSecret = System.Configuration.ConfigurationManager.AppSettings["appSecret"];
var tenantID = System.Configuration.ConfigurationManager.AppSettings["tenantId"];
Console.WriteLine("Init....");
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(appID, appSecret, tenantID, AzureEnvironment.AzureGlobalCloud);
Console.WriteLine("Authenticating...");
azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
Console.WriteLine("Authenticated...");
}
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:
public void CreateVirtualMachine(string vmName, string vmSize,
string vmImage, string vmUserName,
string vmPassword, string resourceGrp,
string avSetName,
string ipSetName, string ipRange,
string subNetRange, string subnetName,
string vNetName, string nicName, string prodSubNetRange)
{
//resetting error providers
passwordError.SetError(txtPassword, string.Empty);
//Add Values to Dictionary
Dictionary<string, string> subnetRanges = new Dictionary<string, string>();
subnetRanges.Add(subnetName + "_Dev", subNetRange);
subnetRanges.Add(subnetName + "_Prod", prodSubNetRange);
try
{
var location = Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral;
Console.WriteLine("Creating resource group...");
var resourceGroup = azure.ResourceGroups.Define(resourceGrp)
.WithRegion(location)
.Create();
Console.WriteLine("Creating availability set...");
var availabilitySet = azure.AvailabilitySets.Define(avSetName)
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithSku(AvailabilitySetSkuTypes.Managed)
.Create();
Console.WriteLine("Creating Dev public IP address...");
var publicIPAddressDev = azure.PublicIPAddresses.Define(ipSetName + "_Dev")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithDynamicIP()
.Create();
Console.WriteLine("Creating Production public IP address...");
var publicIPAddressProd = azure.PublicIPAddresses.Define(ipSetName + "_Prod")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithDynamicIP()
.Create();
Console.WriteLine("Creating virtual network...");
var network = azure.Networks.Define(vNetName)
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithAddressSpace(ipRange)
.WithSubnets(subnetRanges)
.Create();
Console.WriteLine("Creating Development network interface...");
var networkInterfaceDev = azure.NetworkInterfaces.Define(nicName + "_Development")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName + "_Dev")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddressDev)
.Create();
Console.WriteLine("Creating Production network interface...");
var networkInterfaceProd = azure.NetworkInterfaces.Define(nicName + "_Production")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetwork(network)
.WithSubnet(subnetName + "_Prod")
.WithPrimaryPrivateIPAddressDynamic()
.WithExistingPrimaryPublicIPAddress(publicIPAddressProd)
.Create();
Console.WriteLine("Creating development virtual machine...");
azure.VirtualMachines.Define(vmName + "Dev")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetworkInterface(networkInterfaceDev)
.WithLatestWindowsImage(drpImage.Text.Split('/')[0], drpImage.Text.Split('/')[1], drpImage.Text.Split('/')[2])
.WithAdminUsername(vmUserName)
.WithAdminPassword(vmPassword)
.WithComputerName(vmName + "Dev")
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardDS1V2)
.Create();
Console.WriteLine("Creating production virtual machine...");
azure.VirtualMachines.Define(vmName + "Prod")
.WithRegion(location)
.WithExistingResourceGroup(resourceGrp)
.WithExistingPrimaryNetworkInterface(networkInterfaceProd)
.WithLatestWindowsImage(drpImage.Text.Split('/')[0], drpImage.Text.Split('/')[1], drpImage.Text.Split('/')[2])
.WithAdminUsername(vmUserName)
.WithAdminPassword(vmPassword)
.WithComputerName(vmName + "Prod")
.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
// List Down Created Development VM
var devVm = azure.VirtualMachines.GetByResourceGroup(resourceGrp, vmName + "Dev");
Console.WriteLine(" ");
Console.WriteLine("================================================================");
Console.WriteLine("Details about Development VM");
Console.WriteLine("================================================================");
Console.WriteLine("Getting information about the virtual machine...");
Console.WriteLine("hardwareProfile");
Console.WriteLine(" vmSize: " + devVm.Size);
Console.WriteLine("storageProfile");
Console.WriteLine(" imageReference");
Console.WriteLine(" publisher: " + devVm.StorageProfile.ImageReference.Publisher);
Console.WriteLine(" offer: " + devVm.StorageProfile.ImageReference.Offer);
Console.WriteLine(" sku: " + devVm.StorageProfile.ImageReference.Sku);
Console.WriteLine(" version: " + devVm.StorageProfile.ImageReference.Version);
Console.WriteLine(" osDisk");
Console.WriteLine(" osType: " + devVm.StorageProfile.OsDisk.OsType);
Console.WriteLine(" name: " + devVm.StorageProfile.OsDisk.Name);
Console.WriteLine(" createOption: " + devVm.StorageProfile.OsDisk.CreateOption);
Console.WriteLine(" caching: " + devVm.StorageProfile.OsDisk.Caching);
Console.WriteLine("osProfile");
Console.WriteLine(" computerName: " + devVm.OSProfile.ComputerName);
Console.WriteLine(" adminUsername: " + devVm.OSProfile.AdminUsername);
Console.WriteLine(" provisionVMAgent: " + devVm.OSProfile.WindowsConfiguration.ProvisionVMAgent.Value);
Console.WriteLine(" enableAutomaticUpdates: " + devVm.OSProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
Console.WriteLine("networkProfile");
foreach (string nicId in devVm.NetworkInterfaceIds)
{
Console.WriteLine(" networkInterface id: " + nicId);
}
Console.WriteLine("vmAgent");
Console.WriteLine(" vmAgentVersion" + devVm.InstanceView.VmAgent.VmAgentVersion);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus stat in devVm.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 devVm.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: " + devVm.ProvisioningState);
Console.WriteLine(" id: " + devVm.Id);
Console.WriteLine(" name: " + devVm.Name);
Console.WriteLine(" type: " + devVm.Type);
Console.WriteLine(" location: " + devVm.Region);
Console.WriteLine("VM instance status");
foreach (InstanceViewStatus stat in devVm.InstanceView.Statuses)
{
Console.WriteLine(" code: " + stat.Code);
Console.WriteLine(" level: " + stat.Level);
Console.WriteLine(" displayStatus: " + stat.DisplayStatus);
}
// List Down Created Production VMs'
var ProdVm = azure.VirtualMachines.GetByResourceGroup(resourceGrp, vmName + "Prod");
Console.WriteLine(" ");
Console.WriteLine("================================================================");
Console.WriteLine("Details about Production VM");
Console.WriteLine("================================================================");
Console.WriteLine("Getting information about the virtual machine...");
Console.WriteLine("hardwareProfile");
Console.WriteLine(" vmSize: " + ProdVm.Size);
Console.WriteLine("storageProfile");
Console.WriteLine(" imageReference");
Console.WriteLine(" publisher: " + ProdVm.StorageProfile.ImageReference.Publisher);
Console.WriteLine(" offer: " + ProdVm.StorageProfile.ImageReference.Offer);
Console.WriteLine(" sku: " + ProdVm.StorageProfile.ImageReference.Sku);
Console.WriteLine(" version: " + ProdVm.StorageProfile.ImageReference.Version);
Console.WriteLine(" osDisk");
Console.WriteLine(" osType: " + ProdVm.StorageProfile.OsDisk.OsType);
Console.WriteLine(" name: " + ProdVm.StorageProfile.OsDisk.Name);
Console.WriteLine(" createOption: " + ProdVm.StorageProfile.OsDisk.CreateOption);
Console.WriteLine(" caching: " + ProdVm.StorageProfile.OsDisk.Caching);
Console.WriteLine("osProfile");
Console.WriteLine(" computerName: " + ProdVm.OSProfile.ComputerName);
Console.WriteLine(" adminUsername: " + ProdVm.OSProfile.AdminUsername);
Console.WriteLine(" provisionVMAgent: " + ProdVm.OSProfile.WindowsConfiguration.ProvisionVMAgent.Value);
Console.WriteLine(" enableAutomaticUpdates: " + ProdVm.OSProfile.WindowsConfiguration.EnableAutomaticUpdates.Value);
Console.WriteLine("networkProfile");
foreach (string nicId in ProdVm.NetworkInterfaceIds)
{
Console.WriteLine(" networkInterface id: " + nicId);
}
Console.WriteLine("vmAgent");
Console.WriteLine(" vmAgentVersion" + ProdVm.InstanceView.VmAgent.VmAgentVersion);
Console.WriteLine(" statuses");
foreach (InstanceViewStatus stat in ProdVm.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 devVm.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: " + devVm.ProvisioningState);
Console.WriteLine(" id: " + ProdVm.Id);
Console.WriteLine(" name: " + ProdVm.Name);
Console.WriteLine(" type: " + ProdVm.Type);
Console.WriteLine(" location: " + ProdVm.Region);
Console.WriteLine("VM instance status");
foreach (InstanceViewStatus stat in ProdVm.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();
}
catch (Microsoft.Rest.Azure.CloudException cex)
{
Console.WriteLine(cex.Message);
passwordError.SetError(txtPassword, cex.Message);
}
}
public void PopulateMachineSizes()
{
//Populate Machine Sizes
Console.WriteLine("Loading Machine Sizes.....");
var machineSizes = azure
.VirtualMachines
.Sizes
.ListByRegion(Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral);
foreach (var machinesize in machineSizes)
{
drpSize.Items.Add(machinesize.Name);
}
Console.WriteLine("Machine sizes loaded");
}
public void PopulateImages()
{
Console.WriteLine("Loading OS Images.....");
var images = azure
.VirtualMachineImages
.Publishers
.ListByRegion(Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USCentral);
foreach (var image in images)
{
if (StringComparer.OrdinalIgnoreCase.Equals(image.Name, "MicrosoftWindowsServer"))
{
foreach (var offer in image.Offers.List())
{
foreach (var sku in offer.Skus.List())
{
foreach (var img in sku.Images.List())
{
drpImage.Items.Add($"{image.Name }/{offer.Name}/{sku.Name}/{img.Version}");
}
}
}
}
}
Console.WriteLine("OS Images loaded");
}
public void Authenticate()
{
var appID = System.Configuration.ConfigurationManager.AppSettings["appId"];
var appSecret = System.Configuration.ConfigurationManager.AppSettings["appSecret"];
var tenantID = System.Configuration.ConfigurationManager.AppSettings["tenantId"];
Console.WriteLine("Init....");
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal(appID, appSecret, tenantID, AzureEnvironment.AzureGlobalCloud);
Console.WriteLine("Authenticating...");
azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
Console.WriteLine("Authenticated...");
}
Comments
Post a Comment