protected override void ExecuteCommand() { base.ExecuteCommand(); ServiceManagementProfile.Initialize(); if (this.CurrentDeploymentNewSM == null) { return; } string[] inputRoleNames = (this.ParameterSetName == "ByName") ? this.Name : this.VM.Select(vm => vm.GetInstance().RoleName).ToArray(); // Generate a list of role names matching regular expressions or // the exact name specified in the -Name parameter. var roleNames = PersistentVMHelper.GetRoleNames(this.CurrentDeploymentNewSM.RoleInstances, inputRoleNames); // Insure at least one of the role name instances can be found. if ((roleNames == null) || (!roleNames.Any())) { throw new ArgumentOutOfRangeException(String.Format(Resources.RoleInstanceCanNotBeFoundWithName, this.Name)); } // Insure the Force switch is specified for wildcard operations when StayProvisioned is not specified. if (roleNames.Any(r => WildcardPattern.ContainsWildcardCharacters(r)) && (!this.StayProvisioned.IsPresent) && (!this.Force.IsPresent)) { throw new ArgumentException(Resources.MustSpecifyForceParameterWhenUsingWildcards); } if (roleNames.Count == 1) { if (this.StayProvisioned.IsPresent) { ProcessStaticIPAddressWarningInfo(roleNames[0]); this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.Shutdown( this.ServiceName, this.CurrentDeploymentNewSM.Name, roleNames[0], new VirtualMachineShutdownParameters { PostShutdownAction = PostShutdownAction.Stopped }), (s, response) => this.ContextFactory<OperationStatusResponse, ManagementOperationContext>(response, s)); } else { if (!this.Force.IsPresent && this.IsLastVmInDeployment(roleNames.Count)) { this.ConfirmAction(false, Resources.DeploymentVIPLossWarning, string.Format(Resources.DeprovisioningVM, string.Join(", ", roleNames)), String.Empty, () => this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.Shutdown( this.ServiceName, this.CurrentDeploymentNewSM.Name, roleNames[0], new VirtualMachineShutdownParameters { PostShutdownAction = PostShutdownAction.StoppedDeallocated }), (s, response) => ContextFactory<OperationStatusResponse, ManagementOperationContext>(response, s))); } else { this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.Shutdown( this.ServiceName, this.CurrentDeploymentNewSM.Name, roleNames[0], new VirtualMachineShutdownParameters { PostShutdownAction = PostShutdownAction.StoppedDeallocated }), (s, response) => this.ContextFactory<OperationStatusResponse, ManagementOperationContext>(response, s)); } } } else { if (this.StayProvisioned.IsPresent) { var parameter = new VirtualMachineShutdownRolesParameters(); foreach (var role in roleNames) { ProcessStaticIPAddressWarningInfo(role); parameter.Roles.Add(role); } parameter.PostShutdownAction = PostShutdownAction.Stopped; this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.ShutdownRoles(this.ServiceName, this.CurrentDeploymentNewSM.Name, parameter)); } else { var parameter = new VirtualMachineShutdownRolesParameters(); foreach (var role in roleNames) { parameter.Roles.Add(role); } parameter.PostShutdownAction = PostShutdownAction.StoppedDeallocated; if (!this.Force.IsPresent && this.IsLastVmInDeployment(roleNames.Count)) { this.ConfirmAction(false, Resources.DeploymentVIPLossWarning, string.Format(Resources.DeprovisioningVM, string.Join(", ", roleNames)), String.Empty, () => this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.ShutdownRoles(this.ServiceName, this.CurrentDeploymentNewSM.Name, parameter))); } else { this.ExecuteClientActionNewSM( null, this.CommandRuntime.ToString(), () => this.ComputeClient.VirtualMachines.ShutdownRoles(this.ServiceName, this.CurrentDeploymentNewSM.Name, parameter)); } } } }
/// <summary> /// The Shutdown Roles operation stops the specified set of virtual /// machines. /// </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='parameters'> /// The set of virtual machine roles to shutdown and their post /// shutdown state. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task<OperationResponse> BeginShuttingDownRolesAsync(this IVirtualMachineOperations operations, string serviceName, string deploymentName, VirtualMachineShutdownRolesParameters parameters) { return operations.BeginShuttingDownRolesAsync(serviceName, deploymentName, parameters, CancellationToken.None); }
/// <summary> /// The Begin Shutting Down Roles operation stops the specified set of /// virtual machines. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/dn469421.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='parameters'> /// Required. Parameters to pass to the Begin Shutting Down Roles /// operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse BeginShuttingDownRoles(this IVirtualMachineOperations operations, string serviceName, string deploymentName, VirtualMachineShutdownRolesParameters parameters) { return Task.Factory.StartNew((object s) => { return ((IVirtualMachineOperations)s).BeginShuttingDownRolesAsync(serviceName, deploymentName, parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult(); }
/// <summary> /// The Shutdown Roles operation stops the specified set of virtual /// machines. /// </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='parameters'> /// The set of virtual machine roles to shutdown and their post /// shutdown state. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse BeginShuttingDownRoles(this IVirtualMachineOperations operations, string serviceName, string deploymentName, VirtualMachineShutdownRolesParameters parameters) { try { return operations.BeginShuttingDownRolesAsync(serviceName, deploymentName, parameters).Result; } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 1) { throw; } else { throw ex.InnerException; } } }
protected PSArgument[] CreateVirtualMachineShutdownRolesParameters() { string serviceName = string.Empty; string deploymentName = string.Empty; VirtualMachineShutdownRolesParameters parameters = new VirtualMachineShutdownRolesParameters(); return ConvertFromObjectsToArguments(new string[] { "ServiceName", "DeploymentName", "Parameters" }, new object[] { serviceName, deploymentName, parameters }); }