internal async Task <OperationStatusResponse> ShutdownAsync(string serviceName, string deploymentName, string vmName) { var shutdownParams = new VirtualMachineShutdownParameters { PostShutdownAction = PostShutdownAction.StoppedDeallocated }; return(await _computeManagementClient.VirtualMachines.ShutdownAsync(serviceName, deploymentName, vmName, shutdownParams)); }
protected PSArgument[] CreateVirtualMachineShutdownParameters() { 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 })); }
protected void ExecuteVirtualMachineShutdownMethod(object[] invokeMethodInputParameters) { string serviceName = (string)ParseParameter(invokeMethodInputParameters[0]); string deploymentName = (string)ParseParameter(invokeMethodInputParameters[1]); string virtualMachineName = (string)ParseParameter(invokeMethodInputParameters[2]); VirtualMachineShutdownParameters parameters = (VirtualMachineShutdownParameters)ParseParameter(invokeMethodInputParameters[3]); var result = VirtualMachineClient.Shutdown(serviceName, deploymentName, virtualMachineName, parameters); WriteObject(result); }
/// <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); }
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)); } } }