public PSCredential PromptForCredentialInline(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes = PSCredentialTypes.Generic, PSCredentialUIOptions options = PSCredentialUIOptions.None) { Collection <FieldDescription> fields; // NOTE: I'm not sure this is the right action for the PromptForCredential targetName if (!String.IsNullOrEmpty(targetName)) { caption = $"Credential for {targetName}\n\n{caption}"; } if ((options & PSCredentialUIOptions.ReadOnlyUserName) == PSCredentialUIOptions.Default) { var user = new FieldDescription("User"); user.SetParameterType(typeof(string)); user.Label = "Username"; user.DefaultValue = PSObject.AsPSObject(userName); user.IsMandatory = true; do { fields = new Collection <FieldDescription>(new[] { user }); var username = new PromptForObjectEventArgs(caption, message, fields); var login = OnPromptForObject(username); userName = login["User"].BaseObject as string; } while (userName != null && userName.Length == 0); } // I think this is all I can do with the allowedCredentialTypes // domain required if (allowedCredentialTypes > PSCredentialTypes.Generic) { // and no domain if (userName != null && userName.IndexOfAny(new[] { '\\', '@' }) < 0) { userName = $"{targetName}\\{userName}"; } } var pass = new FieldDescription("Password"); pass.SetParameterType(typeof(SecureString)); pass.Label = "Password for " + userName; pass.IsMandatory = true; fields = new Collection <FieldDescription>(new[] { pass }); var pwd = new PromptForObjectEventArgs(string.Empty, string.Empty, fields); var password = OnPromptForObject(pwd); // TODO: I'm not sure what to do with the PSCredentialUIOptions options, because PowerShell.exe ignores them return(new PSCredential(userName, (SecureString)password["Password"].BaseObject)); }
public Dictionary <string, PSObject> OnPromptForObject(PromptForObjectEventArgs e) { EventHandler <PromptForObjectEventArgs> handler = PromptForObject; if (handler != null) { handler(this, e); return(e.Results); } if (!string.IsNullOrEmpty(e.Caption)) { Write(e.Caption + "\n"); } if (!string.IsNullOrEmpty(e.Message)) { Write(e.Message + "\n"); } var results = new Dictionary <string, PSObject>(); foreach (var fd in e.Descriptions) { Type type = Type.GetType(fd.ParameterAssemblyFullName); string prompt = string.IsNullOrEmpty(fd.Label) ? fd.Name : fd.Label; if (type != null && type.IsArray) { type = type.GetElementType(); var output = new List <PSObject>(); int count = 0; do { PSObject single = GetSingle(e.Caption, e.Message, $"{prompt}[{count++}]", fd, type); if (single == null) { break; } if (!(single.BaseObject is string) || ((string)single.BaseObject).Length > 0) { output.Add(single); } else { break; } } while (true); results[fd.Name] = PSObject.AsPSObject(output.ToArray()); } else { var value = GetSingle(e.Caption, e.Message, prompt, fd, type); if (value != null) { results[fd.Name] = value; } } } return(results); }