/// <summary> /// The Shutdown Role operation shuts down the specified virtual /// machine. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157195.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations. /// </param> /// <param name='serviceName'> /// The name of your service. /// </param> /// <param name='deploymentName'> /// The name of your deployment. /// </param> /// <param name='virtualMachineName'> /// The name of the virtual machine to shutdown. /// </param> /// <param name='parameters'> /// The parameters for the shutdown vm operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse BeginShutdown(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineShutdownParameters parameters) { try { return operations.BeginShutdownAsync(serviceName, deploymentName, virtualMachineName, parameters).Result; } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 1) { throw; } else { throw ex.InnerException; } } }
/// <summary> /// The Shutdown Role operation shuts down the specified virtual /// machine. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157195.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations. /// </param> /// <param name='serviceName'> /// The name of your service. /// </param> /// <param name='deploymentName'> /// The name of your deployment. /// </param> /// <param name='virtualMachineName'> /// The name of the virtual machine to shutdown. /// </param> /// <param name='parameters'> /// The parameters for the shutdown vm operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task<OperationResponse> BeginShutdownAsync(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineShutdownParameters parameters) { return operations.BeginShutdownAsync(serviceName, deploymentName, virtualMachineName, parameters, CancellationToken.None); }
/// <summary> /// The Shutdown Role operation shuts down the specified virtual /// machine. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj157195.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.Compute.IVirtualMachineOperations. /// </param> /// <param name='serviceName'> /// Required. The name of your service. /// </param> /// <param name='deploymentName'> /// Required. The name of your deployment. /// </param> /// <param name='virtualMachineName'> /// Required. The name of the virtual machine to shutdown. /// </param> /// <param name='parameters'> /// Required. The parameters for the shutdown vm operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse BeginShutdown(this IVirtualMachineOperations operations, string serviceName, string deploymentName, string virtualMachineName, VirtualMachineShutdownParameters parameters) { return Task.Factory.StartNew((object s) => { return ((IVirtualMachineOperations)s).BeginShutdownAsync(serviceName, deploymentName, virtualMachineName, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); }
/// <summary> /// Shutdown the Virtual Machine. /// </summary> /// <param name="deallocate">Specify deallocate = true to completely deallocate the VM resources and stop billing</param> /// <returns></returns> internal async Task StopVirtualMachine(bool deallocate) { var shutdownParams = new VirtualMachineShutdownParameters(); if (deallocate) shutdownParams.PostShutdownAction = PostShutdownAction.StoppedDeallocated; // Fully deallocate resources and stop billing else shutdownParams.PostShutdownAction = PostShutdownAction.Stopped; // Just put the machine in stopped state, keeping resources allocated await _computeManagementClient.VirtualMachines.ShutdownAsync(_parameters.CloudServiceName, _parameters.CloudServiceName, _parameters.VMName, shutdownParams); }
/// <author>Bart</author> /// <summary> /// Deze methode stopt een Virtual Machine. /// </summary> /// <param name="serviceName">Is de naam van de CloudService waar de VM op staat.</param> public void StopVM(string serviceName, string virtualMachineName) { ComputeManagementClient client = new ComputeManagementClient(cloudCredentials); VirtualMachineShutdownParameters parameters = new VirtualMachineShutdownParameters { PostShutdownAction = PostShutdownAction.StoppedDeallocated }; client.VirtualMachines.BeginShutdown(serviceName, "VMs", virtualMachineName, parameters); System.Diagnostics.Debug.Write(virtualMachineName + " is begonnen met stoppen."); }
public async Task StartStopVirtualMachineAsync(string virtualMachineName, string cloudServiceName, VirtualMachineAction action) { using (var computeClient = new ComputeManagementClient(_credentials)) { HostedServiceGetDetailedResponse vm; try { vm = await computeClient.HostedServices.GetDetailedAsync(cloudServiceName); // Console.WriteLine("Found cloud service: " + cloudServiceName); Console.WriteLine(string.Format("Found cloud service: {0}", cloudServiceName)); } catch (Exception) { Console.WriteLine(string.Format("Virtual Machine for [{0}] cloud was not found!", cloudServiceName)); return; } Console.WriteLine(string.Format("Fetching deployment for virtual machine [{0}].", virtualMachineName)); var deployment = vm.Deployments.ToList().First(x => x.Name == virtualMachineName); //var deployment = vm.Deployments.ToList().First(x => x.Name == cloudServiceName); if (deployment == null) { Console.Write(string.Format("Failed to fetch deployment for virtual machine [{0}] Start/Stop will exit and do nothing", virtualMachineName)); return; } var deploymantSlotName = deployment.Name; var serviceName = vm.ServiceName; Console.WriteLine("Fetching instance."); // GSUHackfest Note #1 by Brent - April 30th // the line that has been commented out worked for Gabriele's tests with a machine he // provisioned via SCAMP. But it didn't work with VMs deployed via the Azure portal. // we'll need to revist this later to try and reconcile the differences. //var instance = deployment.RoleInstances.First(x => x.HostName == virtualMachineName); var instance = deployment.RoleInstances.First(x => x.RoleName == virtualMachineName); Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state", virtualMachineName, instance.InstanceStatus)); Console.WriteLine(string.Format("Machine Name[{0}] is currently at [{1}] state (if not at ReadyRole or StoppedVM the following start/stop will fail)", virtualMachineName, instance.InstanceStatus)); if (action == VirtualMachineAction.Start) { if (instance.InstanceStatus == "ReadyRole") { Console.WriteLine(string.Format("VM [{0}] Deploymentslot[{1}] roleName [{2}] already started (no start will be execute)", serviceName, deploymantSlotName, instance.RoleName)); return; } Console.WriteLine(string.Format("Issuing Management Start cmd Service[{0}] Deploymentslot[{1}] roleName [{2}]", serviceName, deploymantSlotName, instance.RoleName)); //TODO this is strange but for now i leave it a is. Need to be refactored. // refer to "GSUHackfest Note #1" above //await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.HostName); Console.WriteLine(string.Format("Machine Name[{0}] Starting..", virtualMachineName)); await computeClient.VirtualMachines.StartAsync(serviceName, deploymantSlotName, instance.RoleName); Console.WriteLine(string.Format("Machine Name[{0}] start command issued..", virtualMachineName)); } else { if (instance.InstanceStatus == "StoppedVM" || instance.InstanceStatus == "StoppedDeallocated") { Console.WriteLine(string.Format("VM [{0}] Deploymentslot[{1}] roleName [{2}] already stopped (no stop will be execute)", serviceName, deploymantSlotName, instance.RoleName)); return; } // ensures no compute charges for the stopped VM VirtualMachineShutdownParameters shutdownParms = new VirtualMachineShutdownParameters(); shutdownParms.PostShutdownAction = PostShutdownAction.StoppedDeallocated; // refer to "GSUHackfest Note #1" above //computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.HostName, shutdownParms); // computeClient.VirtualMachines.Shutdown(serviceName, deploymantSlotName, instance.RoleName, shutdownParms); Console.WriteLine(string.Format("Machine Name[{0}] Stopping..", virtualMachineName)); await computeClient.VirtualMachines.ShutdownAsync(serviceName, deploymantSlotName, instance.RoleName, shutdownParms); Console.WriteLine(string.Format("Machine Name[{0}] stop command issued..", virtualMachineName)); } } }
protected PSArgument[] CreateVirtualMachineBeginShutdownParameters() { string serviceName = string.Empty; string deploymentName = string.Empty; string virtualMachineName = string.Empty; VirtualMachineShutdownParameters parameters = new VirtualMachineShutdownParameters(); return ConvertFromObjectsToArguments(new string[] { "ServiceName", "DeploymentName", "VirtualMachineName", "Parameters" }, new object[] { serviceName, deploymentName, virtualMachineName, parameters }); }