From 196feedded04b43be93423851d61ef0784e2664d Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 16 Nov 2023 14:34:00 -0600 Subject: [PATCH] Load PowerShell module on script execution. Add cmdlet to deserialize/decode variables. --- .../Cmdlets/ConvertFrom-SerializedBase64.cs | 24 +++++++++++++++++++ .../LANCommander.PowerShell.csproj | 1 + .../PowerShell/PowerShellScript.cs | 17 ++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 LANCommander.PowerShell/Cmdlets/ConvertFrom-SerializedBase64.cs diff --git a/LANCommander.PowerShell/Cmdlets/ConvertFrom-SerializedBase64.cs b/LANCommander.PowerShell/Cmdlets/ConvertFrom-SerializedBase64.cs new file mode 100644 index 0000000..055aeee --- /dev/null +++ b/LANCommander.PowerShell/Cmdlets/ConvertFrom-SerializedBase64.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Management.Automation; +using System.Text; +using System.Threading.Tasks; + +namespace LANCommander.PowerShell.Cmdlets +{ + [Cmdlet(VerbsData.ConvertFrom, "SerializedBase64")] + [OutputType(typeof(object))] + public class ConvertFromSerializedBase64Cmdlet : PSCmdlet + { + [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] + public string Input { get; set; } + + protected override void ProcessRecord() + { + var xml = Encoding.UTF8.GetString(Convert.FromBase64String(Input)); + + WriteObject(PSSerializer.Deserialize(xml)); + } + } +} diff --git a/LANCommander.PowerShell/LANCommander.PowerShell.csproj b/LANCommander.PowerShell/LANCommander.PowerShell.csproj index 9f7d67d..2357de7 100644 --- a/LANCommander.PowerShell/LANCommander.PowerShell.csproj +++ b/LANCommander.PowerShell/LANCommander.PowerShell.csproj @@ -46,6 +46,7 @@ + diff --git a/LANCommander.SDK/PowerShell/PowerShellScript.cs b/LANCommander.SDK/PowerShell/PowerShellScript.cs index 6f300f9..1d86f51 100644 --- a/LANCommander.SDK/PowerShell/PowerShellScript.cs +++ b/LANCommander.SDK/PowerShell/PowerShellScript.cs @@ -18,18 +18,21 @@ namespace LANCommander.SDK.PowerShell private bool IgnoreWow64 { get; set; } = false; private ICollection Variables { get; set; } private Dictionary Arguments { get; set; } + private List Modules { get; set; } private Process Process { get; set; } public PowerShellScript() { Variables = new List(); Arguments = new Dictionary(); + Modules = new List(); Process = new Process(); Process.StartInfo.FileName = "powershell.exe"; Process.StartInfo.RedirectStandardOutput = false; AddArgument("ExecutionPolicy", "Unrestricted"); + AddModule(Path.Combine(Environment.CurrentDirectory, "LANCommander.PowerShell.psd1")); } public PowerShellScript UseFile(string path) @@ -88,6 +91,13 @@ namespace LANCommander.SDK.PowerShell return this; } + public PowerShellScript AddModule(string path) + { + Modules.Add(path); + + return this; + } + public PowerShellScript RunAsAdmin() { AsAdmin = true; @@ -111,9 +121,14 @@ namespace LANCommander.SDK.PowerShell var wow64Value = IntPtr.Zero; + foreach (var module in Modules) + { + scriptBuilder.AppendLine($"Import-Module \"{module}\""); + } + foreach (var variable in Variables) { - scriptBuilder.AppendLine($"${variable.Name} = Convert-FromSerializedBase64 \"{Serialize(variable.Value)}\""); + scriptBuilder.AppendLine($"${variable.Name} = ConvertFrom-SerializedBase64 \"{Serialize(variable.Value)}\""); } scriptBuilder.AppendLine(Contents);