コード例 #1
0
ファイル: ReadHostCommand.cs プロジェクト: nickchal/pash
 protected override void BeginProcessing()
 {
     PSHostUserInterface uI = base.Host.UI;
     if (this.prompt != null)
     {
         string str;
         IEnumerator enumerator = LanguagePrimitives.GetEnumerator(this.prompt);
         if (enumerator != null)
         {
             StringBuilder builder = new StringBuilder();
             while (enumerator.MoveNext())
             {
                 string str2 = (string) LanguagePrimitives.ConvertTo(enumerator.Current, typeof(string), CultureInfo.InvariantCulture);
                 if (!string.IsNullOrEmpty(str2))
                 {
                     if (builder.Length > 0)
                     {
                         builder.Append(' ');
                     }
                     builder.Append(str2);
                 }
             }
             str = builder.ToString();
         }
         else
         {
             str = (string) LanguagePrimitives.ConvertTo(this.prompt, typeof(string), CultureInfo.InvariantCulture);
         }
         FieldDescription description = new FieldDescription(str);
         if (this.AsSecureString != 0)
         {
             description.SetParameterType(typeof(SecureString));
         }
         else
         {
             description.SetParameterType(typeof(string));
         }
         Collection<FieldDescription> descriptions = new Collection<FieldDescription> {
             description
         };
         Dictionary<string, PSObject> dictionary = base.Host.UI.Prompt("", "", descriptions);
         if (dictionary != null)
         {
             foreach (PSObject obj2 in dictionary.Values)
             {
                 base.WriteObject(obj2);
             }
         }
     }
     else
     {
         object obj3;
         if (this.AsSecureString != 0)
         {
             obj3 = base.Host.UI.ReadLineAsSecureString();
         }
         else
         {
             obj3 = base.Host.UI.ReadLine();
         }
         base.WriteObject(obj3);
     }
 }
コード例 #2
0
ファイル: UniformUI.cs プロジェクト: pezipink/FarNet
        public override SecureString ReadLineAsSecureString()
        {
            if (Far.Api.UI.IsCommandMode)
            {
                for (; ; )
                {
                    var ui = new UI.ReadLine() { Password = true };
                    if (!ui.Show())
                    {
                        A.AskStopPipeline();
                        continue;
                    }

                    WriteLine("*");
                    return (SecureString)ValueToResult(ui.Text, true).BaseObject;
                }
            }

            const string name = " ";
            var field = new FieldDescription(name);
            field.SetParameterType(typeof(SecureString));
            var fields = new Collection<FieldDescription>() { field };

            var r = Prompt("", "", fields);
            if (r == null)
                return null;

            return (SecureString)r[name].BaseObject;
        }
コード例 #3
0
ファイル: ReadConsoleCmdlet.cs プロジェクト: 40a/PowerShell
        /// <summary>
        ///
        /// Write the prompt, then collect a line of input from the host, then
        /// output it to the output stream.
        ///
        /// </summary>
        protected override void BeginProcessing()
        {
            PSHostUserInterface ui = Host.UI;
            string promptString;

            if (_prompt != null)
            {
                IEnumerator e = LanguagePrimitives.GetEnumerator(_prompt);
                if (e != null)
                {
                    StringBuilder sb = new StringBuilder();

                    while (e.MoveNext())
                    {
                        // The current object might itself be a collection, like a string array, as in read/console "foo","bar","baz"  
                        // If it is, then the PSObject ToString() will take care of it.  We could go on unwrapping collections
                        // forever, but it's a pretty common use case to see a varags confused with an array.

                        string element = (string)LanguagePrimitives.ConvertTo(e.Current, typeof(string), CultureInfo.InvariantCulture);

                        if (!String.IsNullOrEmpty(element))
                        {
                            // Prepend a space if the stringbuilder isn't empty...
                            // We could consider using $OFS here but that's probably more
                            // effort than is really needed...
                            if (sb.Length > 0)
                                sb.Append(' ');
                            sb.Append(element);
                        }
                    }
                    promptString = sb.ToString();
                }
                else
                {
                    promptString = (string)LanguagePrimitives.ConvertTo(_prompt, typeof(string), CultureInfo.InvariantCulture);
                }

                FieldDescription fd = new FieldDescription(promptString);
                if (AsSecureString)
                {
                    fd.SetParameterType(typeof(System.Security.SecureString));
                }
                else
                {
                    fd.SetParameterType(typeof(String));
                }

                Collection<FieldDescription> fdc = new Collection<FieldDescription>();
                fdc.Add(fd);

                Dictionary<string, PSObject> result = Host.UI.Prompt("", "", fdc);
                // Result can be null depending on the host implementation. One typical
                // example of a null return is for a canceled dialog.
                if (result != null)
                {
                    foreach (PSObject o in result.Values)
                    {
                        WriteObject(o);
                    }
                }
            }
            else
            {
                object result;
                if (AsSecureString)
                {
                    result = Host.UI.ReadLineAsSecureString();
                }
                else
                {
                    result = Host.UI.ReadLine();
                }
                WriteObject(result);
            }
        }
コード例 #4
0
        private Collection<FieldDescription> CreatePromptDataStructures(
            Collection<MergedCompiledCommandParameter> missingMandatoryParameters)
        {
            StringBuilder usedHotKeys = new StringBuilder();
            Collection<FieldDescription> fieldDescriptionList = new Collection<FieldDescription>();

            // See if any of the unbound parameters are mandatory

            foreach (MergedCompiledCommandParameter parameter in missingMandatoryParameters)
            {
                ParameterSetSpecificMetadata parameterSetMetadata =
                    parameter.Parameter.GetParameterSetData(_currentParameterSetFlag);

                FieldDescription fDesc = new FieldDescription(parameter.Parameter.Name);

                string helpInfo = null;

                try
                {
                    helpInfo = parameterSetMetadata.GetHelpMessage(Command);
                }
                catch (InvalidOperationException)
                {
                }
                catch (ArgumentException)
                {
                }

                if (!string.IsNullOrEmpty(helpInfo))
                {
                    fDesc.HelpMessage = helpInfo;
                }
                fDesc.SetParameterType(parameter.Parameter.Type);
                fDesc.Label = BuildLabel(parameter.Parameter.Name, usedHotKeys);

                foreach (ValidateArgumentsAttribute vaAttr in parameter.Parameter.ValidationAttributes)
                {
                    fDesc.Attributes.Add(vaAttr);
                }

                foreach (ArgumentTransformationAttribute arAttr in parameter.Parameter.ArgumentTransformationAttributes)
                {
                    fDesc.Attributes.Add(arAttr);
                }

                fDesc.IsMandatory = true;

                fieldDescriptionList.Add(fDesc);
            } // foreach unbound parameter

            return fieldDescriptionList;
        }
コード例 #5
0
 private Collection<FieldDescription> CreatePromptDataStructures(Collection<MergedCompiledCommandParameter> missingMandatoryParameters)
 {
     StringBuilder usedHotKeys = new StringBuilder();
     Collection<FieldDescription> collection = new Collection<FieldDescription>();
     foreach (MergedCompiledCommandParameter parameter in missingMandatoryParameters)
     {
         ParameterSetSpecificMetadata parameterSetData = parameter.Parameter.GetParameterSetData(base._currentParameterSetFlag);
         FieldDescription item = new FieldDescription(parameter.Parameter.Name);
         string helpMessage = null;
         try
         {
             helpMessage = parameterSetData.GetHelpMessage(this.Command);
         }
         catch (InvalidOperationException)
         {
         }
         catch (ArgumentException)
         {
         }
         if (!string.IsNullOrEmpty(helpMessage))
         {
             item.HelpMessage = helpMessage;
         }
         item.SetParameterType(parameter.Parameter.Type);
         item.Label = BuildLabel(parameter.Parameter.Name, usedHotKeys);
         foreach (ValidateArgumentsAttribute attribute in parameter.Parameter.ValidationAttributes)
         {
             item.Attributes.Add(attribute);
         }
         foreach (ArgumentTransformationAttribute attribute2 in parameter.Parameter.ArgumentTransformationAttributes)
         {
             item.Attributes.Add(attribute2);
         }
         item.IsMandatory = true;
         collection.Add(item);
     }
     return collection;
 }
コード例 #6
0
      PSCredential IPSConsole.PromptForCredential(string caption, string message, string userName, string targetName)
      {
         var user = new FieldDescription("User");
         user.SetParameterType(typeof(string));
         user.DefaultValue = PSObject.AsPSObject(userName);
         user.IsMandatory = true;


         var pass = new FieldDescription("Password");
         pass.SetParameterType(typeof(SecureString));
         pass.IsMandatory = true;

         var cred = new Collection<FieldDescription>(new[] { user, pass });

         var login = ((IPSConsole)this).Prompt(caption, message, cred);

         return new PSCredential(
            (string)login["User"].BaseObject,
            (SecureString)login["Password"].BaseObject);
      }
コード例 #7
0
      PSCredential IPSConsole.PromptForCredential(string caption, string message, string userName, string targetName, 
         PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
      {

         Collection<FieldDescription> fields;
         Dictionary<string, PSObject> login, password;

         // NOTE: I'm not sure this is the right action for the PromptForCredential targetName
         if (!String.IsNullOrEmpty(targetName))
         {
            caption = string.Format("Credential for {0}\n\n{1}", targetName, 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});
               login = ((IPSConsole) this).Prompt(caption, message, fields);
               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 = string.Format("{0}\\{1}", 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 });
         password = ((IPSConsole)this).Prompt(String.Empty, String.Empty, fields);

         // 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);
      }
コード例 #8
0
ファイル: FileSystemProvider.cs プロジェクト: nickchal/pash
 private string PromptNewItemType()
 {
     string str = null;
     if (base.Host != null)
     {
         FieldDescription description = new FieldDescription("Type");
         description.SetParameterType(typeof(string));
         Collection<FieldDescription> descriptions = new Collection<FieldDescription> {
             description
         };
         try
         {
             Dictionary<string, PSObject> dictionary = null;
             dictionary = base.Host.UI.Prompt(string.Empty, string.Empty, descriptions);
             if ((dictionary != null) && (dictionary.Count > 0))
             {
                 foreach (PSObject obj2 in dictionary.Values)
                 {
                     return (string) LanguagePrimitives.ConvertTo(obj2, typeof(string), Thread.CurrentThread.CurrentCulture);
                 }
             }
             return str;
         }
         catch (NotImplementedException)
         {
         }
     }
     return str;
 }
コード例 #9
0
ファイル: PoshConsole.cs プロジェクト: Jaykul/PoshConsole
        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);
        }