Exemple #1
0
        /// <summary>
        /// Gets an array of commands that can be run sequentially to set $profile and run the profile commands.
        /// </summary>
        /// <param name="shellId">The id identifying the host or shell used in profile file names.</param>
        /// <param name="useTestProfile">used from test not to overwrite the profile file names from development boxes</param>
        /// <returns></returns>
        internal static PSCommand[] GetProfileCommands(string shellId, bool useTestProfile)
        {
            List <PSCommand> commands = new List <PSCommand>();
            string           allUsersAllHosts, allUsersCurrentHost, currentUserAllHosts, currentUserCurrentHost, dbgShellHost;
            PSObject         dollarProfile;

            HostUtilities.GetProfileObjectData(shellId, useTestProfile, out allUsersAllHosts, out allUsersCurrentHost, out currentUserAllHosts, out currentUserCurrentHost, out dbgShellHost, out dollarProfile);

            PSCommand command = new PSCommand();

            command.AddCommand("set-variable");
            command.AddParameter("Name", "profile");
            command.AddParameter("Value", dollarProfile);
            command.AddParameter("Option", ScopedItemOptions.None);
            commands.Add(command);

            string[] profilePaths = new string[] { allUsersAllHosts, allUsersCurrentHost, currentUserAllHosts, currentUserCurrentHost };
            foreach (string profilePath in profilePaths)
            {
                if (!System.IO.File.Exists(profilePath))
                {
                    continue;
                }
                command = new PSCommand();
                command.AddCommand(profilePath, false);
                commands.Add(command);
            }

            return(commands.ToArray());
        }
Exemple #2
0
 /// <summary>
 /// Gets the object that serves as a value to $profile and the paths on it
 /// </summary>
 /// <param name="shellId">The id identifying the host or shell used in profile file names.</param>
 /// <param name="useTestProfile">used from test not to overwrite the profile file names from development boxes</param>
 /// <param name="allUsersAllHosts">path for all users and all hosts</param>
 /// <param name="currentUserAllHosts">path for current user and all hosts</param>
 /// <param name="allUsersCurrentHost">path for all users current host</param>
 /// <param name="currentUserCurrentHost">path for current user and current host</param>
 /// <param name="dollarProfile">the object that serves as a value to $profile</param>
 /// <returns></returns>
 internal static void GetProfileObjectData(string shellId, bool useTestProfile, out string allUsersAllHosts, out string allUsersCurrentHost, out string currentUserAllHosts, out string currentUserCurrentHost, out string dbgShellHost, out PSObject dollarProfile)
 {
     allUsersAllHosts       = HostUtilities.GetFullProfileFileName(null, false, useTestProfile);
     allUsersCurrentHost    = HostUtilities.GetFullProfileFileName(shellId, false, useTestProfile);
     currentUserAllHosts    = HostUtilities.GetFullProfileFileName(null, true, useTestProfile);
     currentUserCurrentHost = HostUtilities.GetFullProfileFileName(shellId, true, useTestProfile);
     dbgShellHost           = HostUtilities.GetFullDbgShellProfileFileName();
     dollarProfile          = HostUtilities.GetDollarProfile(allUsersAllHosts, allUsersCurrentHost, currentUserAllHosts, currentUserCurrentHost, dbgShellHost);
 }
Exemple #3
0
 /// <summary>
 /// Gets an array of commands that can be run sequentially to set $profile and run the profile commands.
 /// </summary>
 /// <param name="shellId">The id identifying the host or shell used in profile file names.</param>
 /// <returns></returns>
 public static PSCommand[] GetProfileCommands(string shellId)
 {
     return(HostUtilities.GetProfileCommands(shellId, false));
 }
Exemple #4
0
 /// <summary>
 /// Used to get all profile file names for the current or all hosts and for the current or all users.
 /// </summary>
 /// <param name="shellId">null for all hosts, not null for the specified host</param>
 /// <param name="forCurrentUser">false for all users, true for the current user.</param>
 /// <returns>The profile file name matching the parameters.</returns>
 internal static string GetFullProfileFileName(string shellId, bool forCurrentUser)
 {
     return(HostUtilities.GetFullProfileFileName(shellId, forCurrentUser, false));
 }
        /// <summary>
        /// Prompt for credentials.
        /// </summary>
        /// <param name="userName"> name of the user whose creds are to be prompted for. If set to null or empty string, the function will prompt for user name first. </param>
        ///
        /// <param name="targetName"> name of the target for which creds are being collected </param>
        ///
        /// <param name="message"> message to be displayed. </param>
        ///
        /// <param name="caption"> caption for the message. </param>
        ///
        /// <param name="allowedCredentialTypes"> what type of creds can be supplied by the user </param>
        ///
        /// <param name="options"> options that control the cred gathering UI behavior </param>
        ///
        /// <returns> PSCredential object, or null if input was cancelled (or if reading from stdin and stdin at EOF)</returns>
        ///

        public override PSCredential PromptForCredential(
            string caption,
            string message,
            string userName,
            string targetName,
            PSCredentialTypes allowedCredentialTypes,
            PSCredentialUIOptions options)
        {
            if (!PromptUsingConsole())
            {
                IntPtr mainWindowHandle = GetMainWindowHandle();
                return(HostUtilities.CredUIPromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options, mainWindowHandle));
            }
            else
            {
                PSCredential cred           = null;
                SecureString password       = null;
                string       userPrompt     = null;
                string       passwordPrompt = null;

                if (!string.IsNullOrEmpty(caption))
                {
                    // Should be a skin lookup

                    WriteLineToConsole();
                    WriteToConsole(PromptColor, RawUI.BackgroundColor, WrapToCurrentWindowWidth(caption));
                    WriteLineToConsole();
                }

                if (!string.IsNullOrEmpty(message))
                {
                    WriteLineToConsole(WrapToCurrentWindowWidth(message));
                }

                if (string.IsNullOrEmpty(userName))
                {
                    userPrompt = ConsoleHostUserInterfaceSecurityResources.PromptForCredential_User;

                    //
                    // need to prompt for user name first
                    //
                    do
                    {
                        WriteToConsole(userPrompt, true);
                        userName = ReadLine();
                        if (userName == null)
                        {
                            return(null);
                        }
                    }while (userName.Length == 0);
                }

                passwordPrompt = Util.Sprintf(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
                                              );

                //
                // now, prompt for the password
                //
                WriteToConsole(passwordPrompt, true);
                password = ReadLineAsSecureString();
                if (password == null)
                {
                    return(null);
                }
                WriteLineToConsole();

                cred = new PSCredential(userName, password);

                return(cred);
            }
        }