Merge pull request #87 from jjw24/add_shell_plugin_runasadmin

Add option to run as administrator for Shell plugin
This commit is contained in:
Jeremy Wu 2019-12-10 20:27:51 +11:00 committed by GitHub
commit 9855e2edee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 22 deletions

View File

@ -4,6 +4,7 @@
<system:String x:Key="wox_plugin_cmd_relace_winr">Replace Win+R</system:String>
<system:String x:Key="wox_plugin_cmd_leave_cmd_open">Do not close Command Prompt after command execution</system:String>
<system:String x:Key="wox_plugin_cmd_always_run_as_administrator">Always run as administrator</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_name">Shell</system:String>
<system:String x:Key="wox_plugin_cmd_plugin_description">Allows to execute system commands from Wox. Commands should start with ></system:String>
<system:String x:Key="wox_plugin_cmd_cmd_has_been_executed_times">this command has been executed {0} times</system:String>

View File

@ -9,6 +9,7 @@ using WindowsInput.Native;
using Wox.Infrastructure.Hotkey;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Plugin.SharedCommands;
using Application = System.Windows.Application;
using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys;
@ -164,16 +165,15 @@ namespace Wox.Plugin.Shell
{
command = command.Trim();
command = Environment.ExpandEnvironmentVariables(command);
var workingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var runAsAdministratorArg = !runAsAdministrator && !_settings.RunAsAdministrator ? "" : "runas";
ProcessStartInfo info;
if (_settings.Shell == Shell.Cmd)
{
var arguments = _settings.LeaveShellOpen ? $"/k \"{command}\"" : $"/c \"{command}\" & pause";
info = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = arguments,
};
info = ShellCommand.SetProcessStartInfo("cmd.exe", workingDirectory, arguments, runAsAdministratorArg);
}
else if (_settings.Shell == Shell.Powershell)
{
@ -186,11 +186,8 @@ namespace Wox.Plugin.Shell
{
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
}
info = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = arguments
};
info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsAdministratorArg);
}
else if (_settings.Shell == Shell.RunCommand)
{
@ -200,21 +197,17 @@ namespace Wox.Plugin.Shell
var filename = parts[0];
if (ExistInPath(filename))
{
var arguemtns = parts[1];
info = new ProcessStartInfo
{
FileName = filename,
Arguments = arguemtns
};
var arguments = parts[1];
info = ShellCommand.SetProcessStartInfo(filename, workingDirectory, arguments, runAsAdministratorArg);
}
else
{
info = new ProcessStartInfo(command);
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
}
}
else
{
info = new ProcessStartInfo(command);
info = ShellCommand.SetProcessStartInfo(command, verb: runAsAdministratorArg);
}
}
else
@ -222,10 +215,7 @@ namespace Wox.Plugin.Shell
return;
}
info.UseShellExecute = true;
info.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
info.Verb = runAsAdministrator ? "runas" : "";
try
{

View File

@ -7,6 +7,8 @@ namespace Wox.Plugin.Shell
public Shell Shell { get; set; } = Shell.Cmd;
public bool ReplaceWinR { get; set; } = true;
public bool LeaveShellOpen { get; set; }
public bool RunAsAdministrator { get; set; } = true;
public Dictionary<string, int> Count = new Dictionary<string, int>();
public void AddCmdHistory(string cmdName)

View File

@ -12,10 +12,12 @@
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" x:Name="ReplaceWinR" Content="{DynamicResource wox_plugin_cmd_relace_winr}" Margin="10" HorizontalAlignment="Left"/>
<CheckBox Grid.Row="1" x:Name="LeaveShellOpen" Content="{DynamicResource wox_plugin_cmd_leave_cmd_open}" Margin="10" HorizontalAlignment="Left"/>
<ComboBox Grid.Row="2" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<CheckBox Grid.Row="2" x:Name="AlwaysRunAsAdministrator" Content="{DynamicResource wox_plugin_cmd_always_run_as_administrator}" Margin="10" HorizontalAlignment="Left"/>
<ComboBox Grid.Row="3" x:Name="ShellComboBox" Margin="10" HorizontalAlignment="Left" >
<ComboBoxItem>CMD</ComboBoxItem>
<ComboBoxItem>PowerShell</ComboBoxItem>
<ComboBoxItem>RunCommand</ComboBoxItem>

View File

@ -17,6 +17,7 @@ namespace Wox.Plugin.Shell
{
ReplaceWinR.IsChecked = _settings.ReplaceWinR;
LeaveShellOpen.IsChecked = _settings.LeaveShellOpen;
AlwaysRunAsAdministrator.IsChecked = _settings.RunAsAdministrator;
LeaveShellOpen.IsEnabled = _settings.Shell != Shell.RunCommand;
LeaveShellOpen.Checked += (o, e) =>
@ -29,6 +30,16 @@ namespace Wox.Plugin.Shell
_settings.LeaveShellOpen = false;
};
AlwaysRunAsAdministrator.Checked += (o, e) =>
{
_settings.RunAsAdministrator = true;
};
AlwaysRunAsAdministrator.Unchecked += (o, e) =>
{
_settings.RunAsAdministrator = false;
};
ReplaceWinR.Checked += (o, e) =>
{
_settings.ReplaceWinR = true;

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wox.Plugin.SharedCommands
{
public static class ShellCommand
{
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory="", string arguments = "", string verb = "")
{
var info = new ProcessStartInfo
{
FileName = fileName,
WorkingDirectory = workingDirectory,
Arguments = arguments,
Verb = verb
};
return info;
}
}
}

View File

@ -78,6 +78,7 @@
<Compile Include="Result.cs" />
<Compile Include="ActionContext.cs" />
<Compile Include="SharedCommands\SearchWeb.cs" />
<Compile Include="SharedCommands\ShellCommand.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />