public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) { var promptInput = new EdgePromptInput() { Caption = caption, Message = message, FieldDescriptions = new List <EdgeFieldDescription>() }; promptInput.FieldDescriptions.Add(new EdgeFieldDescription() { Name = "UserName", IsArray = false, IsSecureString = false, Type = "String" }); promptInput.FieldDescriptions.Add(new EdgeFieldDescription() { Name = "Password", IsArray = false, IsSecureString = true, Type = "SecureString" }); var output = this.promptHandler(promptInput).Result; var outputDict = (IDictionary <string, object>)output; var user = string.Empty; var password = string.Empty; foreach (var entry in (object[])outputDict["Results"]) { var entryDict = (IDictionary <string, object>)entry; if (entryDict["Name"].Equals("UserName")) { user = (string)entryDict["Value"]; } if (entryDict["Name"].Equals("Password")) { password = (string)entryDict["Value"]; } } return(new PSCredential(user, ToSecureString(password))); }
public override int PromptForChoice(string caption, string message, Collection <ChoiceDescription> choices, int defaultChoice) { var promptInput = new EdgePromptInput() { Caption = caption, Message = message, FieldDescriptions = new List <EdgeFieldDescription>() }; // Convert the choice collection into something that is // easier to work with. See the BuildHotkeysAndPlainLabels // method for details. string[,] promptData = BuildHotkeysAndPlainLabels(choices); // Format the overall choice prompt string to display. StringBuilder sb = new StringBuilder(); for (int element = 0; element < choices.Count; element++) { sb.Append(String.Format( CultureInfo.CurrentCulture, "[{0}] {1} ", promptData[0, element], promptData[1, element])); } sb.Append(String.Format( CultureInfo.CurrentCulture, "(Default is [\"{0}\"])", promptData[0, defaultChoice])); promptInput.FieldDescriptions.Add(new EdgeFieldDescription() { Name = "Choice", IsArray = false, IsSecureString = false, Type = sb.ToString() }); // Read prompts until a match is made, the default is // chosen, or the loop is interrupted with ctrl-C. while (true) { var output = this.promptHandler(promptInput).Result; var outputDict = (IDictionary <string, object>)output; var choice = string.Empty; foreach (var entry in (object[])outputDict["Results"]) { var entryDict = (IDictionary <string, object>)entry; if (entryDict["Name"].Equals("Choice")) { choice = (string)entryDict["Value"]; } } // If the choice string was empty, use the default selection. if (choice.Length == 0) { return(defaultChoice); } // See if the selection matched and return the // corresponding index if it did. for (int i = 0; i < choices.Count; i++) { if (promptData[0, i].Equals(choice, StringComparison.CurrentCultureIgnoreCase) || promptData[1, i].Equals(choice, StringComparison.CurrentCultureIgnoreCase)) { return(i); } } } }
public override Dictionary <string, PSObject> Prompt(string caption, string message, Collection <FieldDescription> descriptions) { // Maintain the list of all secure strings. These will have to be converted later. var secureStrings = new HashSet <string>(); var promptInput = new EdgePromptInput() { Caption = caption, Message = message, FieldDescriptions = new List <EdgeFieldDescription>() }; var result = new Dictionary <string, PSObject>(); for (int i = 0; i < descriptions.Count; i++) { // Handle credential types. Add a dummy result for these fields without prompting the user in this call. // The PS engine will later call one of the PromptForCredentials callbacks. if (descriptions[i].ParameterTypeFullName.Equals("System.Management.Automation.PSCredential", StringComparison.OrdinalIgnoreCase)) { result[descriptions[i].Name] = ""; continue; } var isSecureString = descriptions[i].ParameterTypeFullName.Contains("System.Security.SecureString"); promptInput.FieldDescriptions.Add(new EdgeFieldDescription() { Name = descriptions[i].Name, Type = descriptions[i].ParameterTypeName, IsArray = descriptions[i].ParameterTypeName.EndsWith("[]"), // TODO: This approach is fast, but may not work for arrays containing generics. IsSecureString = isSecureString }); if (isSecureString) { secureStrings.Add((descriptions[i].Name)); } } if (promptInput.FieldDescriptions.Count > 0) { var output = this.promptHandler(promptInput).Result; var outputDict = (IDictionary <string, object>)output; foreach (var entry in (object[])outputDict["Results"]) { var entryDict = (IDictionary <string, object>)entry; var fieldName = (string)entryDict["Name"]; var fieldValue = entryDict["Value"]; if (secureStrings.Contains(fieldName)) { var singleStr = fieldValue as string; if (singleStr != null) { fieldValue = ToSecureString(singleStr); } var arrayStr = fieldValue as object[]; if (arrayStr != null) { fieldValue = new SecureString[arrayStr.Length]; for (int i = 0; i < arrayStr.Length; i++) { ((SecureString[])fieldValue)[i] = ToSecureString((string)arrayStr[i]); } } } result[fieldName] = PSObject.AsPSObject(fieldValue); } } return(result); }