Run scripts as admin using CLI elevation
This commit is contained in:
parent
9b00cb69fb
commit
85e47f5d3a
8 changed files with 150 additions and 46 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using LANCommander.SDK.Helpers;
|
||||
using LANCommander.SDK.Enums;
|
||||
using LANCommander.SDK.Helpers;
|
||||
using LANCommander.SDK.PowerShell.Cmdlets;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -17,20 +18,22 @@ namespace LANCommander.SDK.PowerShell
|
|||
{
|
||||
public class PowerShellScript
|
||||
{
|
||||
public ScriptType Type { get; private set; }
|
||||
private string Contents { get; set; } = "";
|
||||
private string WorkingDirectory { get; set; } = "";
|
||||
private bool AsAdmin { get; set; } = false;
|
||||
public string WorkingDirectory { get; private set; } = "";
|
||||
private bool ShellExecute { get; set; } = false;
|
||||
public bool RunAsAdmin { get; private set; } = false;
|
||||
private bool IgnoreWow64 { get; set; } = false;
|
||||
private bool Debug { get; set; } = false;
|
||||
private ICollection<PowerShellVariable> Variables { get; set; }
|
||||
private Dictionary<string, string> Arguments { get; set; }
|
||||
public PowerShellVariableList Variables { get; private set; }
|
||||
public Dictionary<string, string> Arguments { get; private set; }
|
||||
|
||||
private InitialSessionState InitialSessionState { get; set; }
|
||||
|
||||
public PowerShellScript()
|
||||
public PowerShellScript(ScriptType type)
|
||||
{
|
||||
Variables = new List<PowerShellVariable>();
|
||||
Type = type;
|
||||
Variables = new PowerShellVariableList();
|
||||
Arguments = new Dictionary<string, string>();
|
||||
|
||||
InitialSessionState = InitialSessionState.CreateDefault();
|
||||
|
|
@ -55,6 +58,9 @@ namespace LANCommander.SDK.PowerShell
|
|||
{
|
||||
Contents = File.ReadAllText(path);
|
||||
|
||||
if (Contents.StartsWith("# Requires Admin"))
|
||||
RunAsAdmin = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -62,6 +68,9 @@ namespace LANCommander.SDK.PowerShell
|
|||
{
|
||||
Contents = contents;
|
||||
|
||||
if (Contents.StartsWith("# Requires Admin"))
|
||||
RunAsAdmin = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -107,13 +116,6 @@ namespace LANCommander.SDK.PowerShell
|
|||
return this;
|
||||
}
|
||||
|
||||
public PowerShellScript RunAsAdmin()
|
||||
{
|
||||
AsAdmin = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public PowerShellScript IgnoreWow64Redirection()
|
||||
{
|
||||
IgnoreWow64 = true;
|
||||
|
|
@ -128,15 +130,19 @@ namespace LANCommander.SDK.PowerShell
|
|||
return this;
|
||||
}
|
||||
|
||||
public PowerShellScript AsAdmin()
|
||||
{
|
||||
RunAsAdmin = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public async Task<int> ExecuteAsync()
|
||||
{
|
||||
var scriptBuilder = new StringBuilder();
|
||||
|
||||
var wow64Value = IntPtr.Zero;
|
||||
|
||||
if (Contents.StartsWith("# Requires Admin"))
|
||||
RunAsAdmin();
|
||||
|
||||
foreach (var variable in Variables)
|
||||
{
|
||||
scriptBuilder.AppendLine($"${variable.Name} = ConvertFrom-SerializedBase64 \"{Serialize(variable.Value)}\"");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue