From 4db1cc8fcccf31d1aa98391f1cb8242574402abb Mon Sep 17 00:00:00 2001
From: Jordi Adoumie <98557455+joadoumie@users.noreply.github.com>
Date: Wed, 23 Oct 2024 15:00:20 -0400
Subject: [PATCH] Added runas admin to Context Menu
---
Directory.Packages.props | 4 +-
.../Commands/LaunchProfileAsAdminCommand.cs | 100 ++++++++++++++++++
.../Pages/ProfilesListPage.cs | 3 +
3 files changed, 105 insertions(+), 2 deletions(-)
create mode 100644 src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs
diff --git a/Directory.Packages.props b/Directory.Packages.props
index df7853744b..45b1f59d94 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -84,7 +84,7 @@
-
+
@@ -100,4 +100,4 @@
-
+
\ No newline at end of file
diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs
new file mode 100644
index 0000000000..9e69b4d968
--- /dev/null
+++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileAsAdminCommand.cs
@@ -0,0 +1,100 @@
+// Copyright (c) Microsoft Corporation
+// The Microsoft Corporation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
+using System.Resources;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.CmdPal.Ext.WindowsTerminal.Helpers;
+using Microsoft.CmdPal.Extensions;
+using Microsoft.CmdPal.Extensions.Helpers;
+using Windows.UI;
+
+namespace Microsoft.CmdPal.Ext.WindowsTerminal.Commands;
+
+internal sealed partial class LaunchProfileAsAdminCommand : InvokableCommand
+{
+ private readonly string _id;
+ private readonly string _profile;
+ private readonly bool _openNewTab;
+ private readonly bool _openQuake;
+
+ internal LaunchProfileAsAdminCommand(string id, string profile, bool openNewTab, bool openQuake)
+ {
+ this._id = id;
+ this._profile = profile;
+ this._openNewTab = openNewTab;
+ this._openQuake = openQuake;
+
+ this.Name = "Launch Profile as Admin";
+ this.Icon = new("\xE7EF");
+ }
+
+ private void LaunchElevated(string id, string profile)
+ {
+ try
+ {
+ var path = "shell:AppsFolder\\" + id;
+ var arguments = TerminalHelper.GetArguments(profile, _openNewTab, _openQuake);
+
+ var startInfo = new System.Diagnostics.ProcessStartInfo
+ {
+ FileName = path,
+ Arguments = arguments,
+ UseShellExecute = true,
+ Verb = "runas",
+ };
+
+ System.Diagnostics.Process.Start(startInfo);
+ }
+#pragma warning disable IDE0059, CS0168, SA1005
+ catch (Exception ex)
+ {
+ // TODO - We need to figure out some logging
+ //var name = "Plugin: " + Resources.plugin_name;
+ //var message = Resources.run_terminal_failed;
+ //Log.Exception("Failed to open Windows Terminal", ex, GetType());
+ //_context.API.ShowMsg(name, message, string.Empty);
+ }
+ }
+#pragma warning restore IDE0059, CS0168, SA1005
+
+ private void Launch(string id, string profile)
+ {
+ var appManager = new ApplicationActivationManager();
+ const ActivateOptions noFlags = ActivateOptions.None;
+ var queryArguments = TerminalHelper.GetArguments(profile, _openNewTab, _openQuake);
+ try
+ {
+ appManager.ActivateApplication(id, queryArguments, noFlags, out var unusedPid);
+ }
+#pragma warning disable IDE0059, CS0168
+ catch (Exception ex)
+ {
+ // TODO: We need to figure out some logging
+ // var name = "Plugin: " + Resources.plugin_name;
+ // var message = Resources.run_terminal_failed;
+ // Log.Exception("Failed to open Windows Terminal", ex, GetType());
+ // _context.API.ShowMsg(name, message, string.Empty);
+ }
+ }
+#pragma warning restore IDE0059, CS0168
+
+ public override CommandResult Invoke()
+ {
+ try
+ {
+ LaunchElevated(_id, _profile);
+ }
+ catch
+ {
+ // TODO: We need to figure out some logging
+ }
+
+ return CommandResult.KeepOpen();
+ }
+}
diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs
index d07f399786..75d2676162 100644
--- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs
+++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs
@@ -52,6 +52,9 @@ internal sealed partial class ProfilesListPage : ListPage
{
Title = profile.Name,
Subtitle = profile.Terminal.DisplayName,
+ MoreCommands = [
+ new CommandContextItem(new LaunchProfileAsAdminCommand(profile.Terminal.AppUserModelId, profile.Name, true, false)),
+ ],
// Icon = () => GetLogo(profile.Terminal),
// Action = _ =>