MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#region Using directives5using Microsoft.Management.Infrastructure.Options;6#endregion7 8namespace Microsoft.Management.Infrastructure.CimCmdlets9{10 /// <summary>11 /// <para>12 /// Prompt user the message coming from provider.13 /// </para>14 /// <para>15 /// At the same time <see cref="CimPromptUser"/> class will prepare the16 /// message for -whatif parameter, while the message represents17 /// what will happen if execute the operation, but not do the operation.18 /// For example, Remove-CimInstance, the whatif message will like,19 /// "CIM Instance: Win32_Process@{Key=1} will be deleted."20 /// </para>21 /// </summary>22 internal sealed class CimPromptUser : CimSyncAction23 {24 /// <summary>25 /// Initializes a new instance of the <see cref="CimPromptUser"/> class.26 /// </summary>27 public CimPromptUser(string message,28 CimPromptType prompt)29 {30 this.Message = message;31 this.prompt = prompt;32 }33 34 /// <summary>35 /// <para>36 /// Prompt user with the given message and prepared whatif message.37 /// </para>38 /// </summary>39 /// <param name="cmdlet">40 /// cmdlet wrapper object, to which write result.41 /// <see cref="CmdletOperationBase"/> for details.42 /// </param>43 public override void Execute(CmdletOperationBase cmdlet)44 {45 ValidationHelper.ValidateNoNullArgument(cmdlet, "cmdlet");46 47 bool yestoall = false;48 bool notoall = false;49 bool result = false;50 51 switch (this.prompt)52 {53 case CimPromptType.Critical:54 // NOTES: prepare the whatif message and caption55 try56 {57 result = cmdlet.ShouldContinue(Message, "caption", ref yestoall, ref notoall);58 if (yestoall)59 {60 this.responseType = CimResponseType.YesToAll;61 }62 else if (notoall)63 {64 this.responseType = CimResponseType.NoToAll;65 }66 else if (result)67 {68 this.responseType = CimResponseType.Yes;69 }70 else if (!result)71 {72 this.responseType = CimResponseType.No;73 }74 }75 catch76 {77 this.responseType = CimResponseType.NoToAll;78 throw;79 }80 finally81 {82 // unblocking the waiting thread83 this.OnComplete();84 }85 86 break;87 case CimPromptType.Normal:88 try89 {90 result = cmdlet.ShouldProcess(Message);91 if (result)92 {93 this.responseType = CimResponseType.Yes;94 }95 else if (!result)96 {97 this.responseType = CimResponseType.No;98 }99 }100 catch101 {102 this.responseType = CimResponseType.NoToAll;103 throw;104 }105 finally106 {107 // unblocking the waiting thread108 this.OnComplete();109 }110 111 break;112 default:113 break;114 }115 116 this.OnComplete();117 }118 119 #region members120 121 /// <summary>122 /// Prompt message.123 /// </summary>124 public string Message { get; }125 126 /// <summary>127 /// Prompt type -Normal or Critical.128 /// </summary>129 private readonly CimPromptType prompt;130 131 #endregion132 }133}134 