mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[PT Run] Run as user feature (#17283)
* run as different user * fix tests * Update src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs * fix typo
This commit is contained in:
@@ -153,19 +153,29 @@ namespace Microsoft.Plugin.Shell
|
||||
return history.ToList();
|
||||
}
|
||||
|
||||
private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdministrator = false)
|
||||
private ProcessStartInfo PrepareProcessStartInfo(string command, RunAsType runAs = RunAsType.None)
|
||||
{
|
||||
string trimmedCommand = command.Trim();
|
||||
command = Environment.ExpandEnvironmentVariables(trimmedCommand);
|
||||
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? string.Empty : "runas";
|
||||
|
||||
// Set runAsArg
|
||||
string runAsVerbArg = string.Empty;
|
||||
if (runAs == RunAsType.OtherUser)
|
||||
{
|
||||
runAsVerbArg = "runAsUser";
|
||||
}
|
||||
else if (runAs == RunAsType.Administrator || _settings.RunAsAdministrator)
|
||||
{
|
||||
runAsVerbArg = "runAs";
|
||||
}
|
||||
|
||||
ProcessStartInfo info;
|
||||
if (_settings.Shell == ExecutionShell.Cmd)
|
||||
{
|
||||
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
|
||||
|
||||
info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsVerbArg);
|
||||
}
|
||||
else if (_settings.Shell == ExecutionShell.Powershell)
|
||||
{
|
||||
@@ -179,14 +189,14 @@ namespace Microsoft.Plugin.Shell
|
||||
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
|
||||
}
|
||||
|
||||
info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsVerbArg);
|
||||
}
|
||||
else if (_settings.Shell == ExecutionShell.RunCommand)
|
||||
{
|
||||
// Open explorer if the path is a file or directory
|
||||
if (Directory.Exists(command) || File.Exists(command))
|
||||
{
|
||||
info = ShellCommand.SetProcessStartInfo("explorer.exe", arguments: command, verb: runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo("explorer.exe", arguments: command, verb: runAsVerbArg);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -197,16 +207,16 @@ namespace Microsoft.Plugin.Shell
|
||||
if (ExistInPath(filename))
|
||||
{
|
||||
var arguments = parts[1];
|
||||
info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsVerbArg);
|
||||
}
|
||||
else
|
||||
{
|
||||
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo(command, verb: runAsVerbArg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
|
||||
info = ShellCommand.SetProcessStartInfo(command, verb: runAsVerbArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -222,6 +232,13 @@ namespace Microsoft.Plugin.Shell
|
||||
return info;
|
||||
}
|
||||
|
||||
private enum RunAsType
|
||||
{
|
||||
None,
|
||||
Administrator,
|
||||
OtherUser,
|
||||
}
|
||||
|
||||
private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
|
||||
{
|
||||
try
|
||||
@@ -326,7 +343,21 @@ namespace Microsoft.Plugin.Shell
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
Action = c =>
|
||||
{
|
||||
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
|
||||
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, RunAsType.Administrator));
|
||||
return true;
|
||||
},
|
||||
},
|
||||
new ContextMenuResult
|
||||
{
|
||||
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
|
||||
Title = Properties.Resources.wox_plugin_cmd_run_as_user,
|
||||
Glyph = "\xE7EE",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.U,
|
||||
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
|
||||
Action = _ =>
|
||||
{
|
||||
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, RunAsType.OtherUser));
|
||||
return true;
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user