示例#1
0
        internal static string[] AugmentCommand(string[] args)
        {
            string currentShellExeName;
            Shell  currentShell = ShellHelper.DetectInvokingShell(out currentShellExeName);

            if (!InputArguments.Direct)
            {
                if (currentShell == Shell.PowerShellCore623BuggedGlobalInstall)
                {
                    // PowerShell Core 6.0.0 to 6.2.3 does not supports command line arguments.

                    // See:
                    // https://github.com/PowerShell/PowerShell/pull/10461#event-2959890147
                    // https://github.com/gerardog/gsudo/issues/10

                    /*
                     * Running ./gsudo from powershell should elevate the current shell, which means:
                     *  => On PowerShell, run => powershell -NoLogo
                     *  => On PowerShellCore => pwsh -NoLogo
                     *  => On PowerShellCore623BuggedGlobalInstall => pwsh
                     *
                     * Running ./gsudo {command}   should elevate the powershell command.
                     *  => On PowerShell => powershell -NoLogo -NoProfile -Command {command}
                     *  => On PowerShellCore => pwsh -NoLogo -NoProfile -Command {command}
                     *  => On PowerShellCore623BuggedGlobalInstall => pwsh {command}
                     */

                    Logger.Instance.Log("Please update to PowerShell Core >= 6.2.4 to avoid profile loading.", LogLevel.Warning);

                    var newArgs = new List <string>();
                    newArgs.Add($"\"{currentShellExeName}\"");
                    newArgs.AddMany(args);

                    return(newArgs.ToArray());
                }
                else if (currentShell.In(Shell.PowerShell, Shell.PowerShellCore))
                {
                    var newArgs = new List <string>();

                    newArgs.Add($"\"{currentShellExeName}\"");
                    newArgs.Add("-NoLogo");

                    if (args.Length > 0)
                    {
                        newArgs.Add("-NoProfile");
                        newArgs.Add("-Command");

                        int last = args.Length - 1;

                        if (args[0].StartsWith("\"", StringComparison.Ordinal) &&
                            args[last].EndsWith("\"", StringComparison.Ordinal))
                        {
                            args[0]    = args[0].Substring(1);
                            args[last] = args[last].Substring(0, args[last].Length - 1);
                        }

                        //-- Fix issue in powershell with commands ending in \" as in "C:\Windows\"
                        if (args[last].EndsWith("\\", StringComparison.Ordinal))
                        {
                            args[last] += "\\";
                        }

                        if (currentShell == Shell.PowerShell) // Windows Powershell extra issues (not core)
                        {
                            //See https://stackoverflow.com/a/59960203/97471
                            for (int i = 0; i < args.Length; i++)
                            {
                                if (args[i].EndsWith("\\\"", StringComparison.Ordinal))
                                {
                                    args[i] = args[i].Substring(0, args[i].Length - 2) + "\\\\\"";
                                }
                            }
                        }
                        // ----

                        string pscommand = string.Join(" ", args);

                        pscommand = pscommand
                                    .Replace("\"", "\\\"")
                                    .Quote();
                        newArgs.Add(pscommand);
                    }

                    return(DoFixIfIsMicrosoftStoreApp(currentShellExeName, newArgs.ToArray()));
                }
                else if (currentShell == Shell.Yori)
                {
                    if (args.Length == 0)
                    {
                        return new[] { currentShellExeName }
                    }
                    ;
                    else
                    {
                        return new[] { currentShellExeName, "-c" }
                    }
示例#2
0
        internal static string[] AugmentCommand(string[] args)
        {
            string currentShellExeName;
            Shell  currentShell = ShellHelper.DetectInvokingShell(out currentShellExeName);

            if (currentShell.In(Shell.PowerShell, Shell.PowerShellCore, Shell.PowerShellCore623BuggedGlobalInstall))
            {
                // PowerShell Core 6.0.0 to 6.2.3 does not supports command line arguments.

                // See:
                // https://github.com/PowerShell/PowerShell/pull/10461#event-2959890147
                // https://github.com/gerardog/gsudo/issues/10

                /*
                 * Running ./gsudo from powershell should elevate the current shell, which means:
                 *  => On PowerShell, run => powershell -NoLogo
                 *  => On PowerShellCore => pwsh -NoLogo
                 *  => On PowerShellCore623BuggedGlobalInstall => pwsh
                 *
                 * Running ./gsudo {command}   should elevate the powershell command.
                 *  => On PowerShell => powershell -NoLogo -NoProfile -Command {command}
                 *  => On PowerShellCore => pwsh -NoLogo -NoProfile -Command {command}
                 *  => On PowerShellCore623BuggedGlobalInstall => pwsh {command}
                 */

                var newArgs = new List <string>();
                newArgs.Add($"\"{currentShellExeName}\"");

                if (currentShell == Shell.PowerShellCore623BuggedGlobalInstall)
                {
                    Logger.Instance.Log("Please update to PowerShell Core >= 6.2.4 to avoid profile loading.", LogLevel.Warning);
                }
                else
                {
                    newArgs.Add("-NoLogo");

                    if (args.Length > 0)
                    {
                        newArgs.Add("-NoProfile");
                        newArgs.Add("-Command");
                    }
                }

                if (args.Length > 0)
                {
                    newArgs.AddMany(args);
                }

                return(newArgs.ToArray());
            }

            if (currentShell == Shell.Yori)
            {
                if (args.Length == 0)
                {
                    return new[] { currentShellExeName }
                }
                ;
                else
                {
                    return new[] { currentShellExeName, "-c" }
                }