예제 #1
0
 public void ResizeVirtualMachine(string virtualMachineName, AzureVmSize size)
 {
     azureLogin.Authenticate();
     using (var computeManagementClient = new ComputeManagementClient(azureLogin.Credentials))
     {
         var vm = new VirtualMachine()
         {
             Name = virtualMachineName,
             Location = demo.Location,
             HardwareProfile = new HardwareProfile()
             {
                 VirtualMachineSize = size,
             },
         };
         HandleResult($"Resize VM '{virtualMachineName}' to {size}", 
             () => computeManagementClient.VirtualMachines.CreateOrUpdate(demo.Group, vm));
     }
 }
예제 #2
0
        public void CreateVirtualMachine(string virtualMachineName, AzureVmSize size, string ipName, bool startVm = true)
        {
            azureLogin.Authenticate();
            using (var computeManagementClient = new ComputeManagementClient(azureLogin.Credentials))
            {
                var osdiskName = virtualMachineName + "_osdisk";

                var vm = new VirtualMachine()
                {
                    Name = virtualMachineName,
                    Location = demo.Location,
                    OSProfile = new OSProfile()
                    {
                        AdminUsername = demo.VmAdminUserName,
                        AdminPassword = demo.VmAdminPassword,
                        ComputerName = virtualMachineName,
                        WindowsConfiguration = new WindowsConfiguration()
                        {
                            EnableAutomaticUpdates = true,
                            ProvisionVMAgent = true,
                        },
                    },
                    HardwareProfile = new HardwareProfile()
                    {
                        VirtualMachineSize = size
                    },
                    NetworkProfile = new NetworkProfile()
                    {
                        NetworkInterfaces = new List<NetworkInterfaceReference>()
                        {
                            new NetworkInterfaceReference
                            {
                                ReferenceUri = GetNicId(ipName).Id,
                            },
                        },
                    },
                    StorageProfile = new StorageProfile()
                    {
                        ImageReference = new ImageReference()
                        {
                            Publisher = "MicrosoftWindowsServer",
                            Offer = "WindowsServer",
                            Sku = "2012-R2-Datacenter",
                            Version = "latest",
                        },
                        OSDisk = new OSDisk
                        {
                            Name = osdiskName,
                            CreateOption = "FromImage",
                            Caching = "ReadWrite",
                            VirtualHardDisk = new VirtualHardDisk()
                            {
                                Uri = string.Format("http://{0}.blob.core.windows.net/vhds/{1}.vhd", demo.Storage, osdiskName),
                            },
                        },
                    },
                };

                HandleResult("Create VM", () => computeManagementClient.VirtualMachines.CreateOrUpdate(demo.Group, vm));

                var vmInstance = HandleResult("Get VM instance " + virtualMachineName,
                    () => computeManagementClient.VirtualMachines.GetWithInstanceView(demo.Group, virtualMachineName),
                    t => t.VirtualMachine.ProvisioningState);
                var stat = vmInstance.VirtualMachine.InstanceView.Statuses.FirstOrDefault(s => s.Code.StartsWith("PowerState"));
                bool vmStarted = stat != null && stat.DisplayStatus == "VM running";

                if (startVm && !vmStarted)
                {
                    StartVirtualMachine(virtualMachineName);
                }
            }
        }