public static string ExecuteCommand(string command, PowerShell rs, CustomPSHost host, bool dontDecode = false, bool silent = false, bool addOutDefault = true) { string output = ""; if (command != null && command.Length > 0) { using (Pipeline pipe = rs.Runspace.CreatePipeline()) { if (!dontDecode) { try { if (ProgramOptions.XorKey != 0) { command = Decoder.XorDecode(Decoder.Base64DecodeBinary(command), ProgramOptions.XorKey); } } catch (Exception e) { if (!silent) { Info($"[-] Could not decode command: {e.Message.ToString()}"); } } } if (!silent) { Info($"PS> {command}"); } pipe.Commands.AddScript(command); pipe.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); if (addOutDefault) { pipe.Commands.Add("Out-default"); } try { pipe.Invoke(); output = ((CustomPSHostUserInterface)host.UI).Output; ((CustomPSHostUserInterface)host.UI)._sb = new StringBuilder(); command = ""; } catch (Exception e) { if (!silent) { Console.WriteLine(e.ToString()); } } } } return(output); }
private static string ExecuteCommand(string command, PowerShell rs, CustomPSHost host, bool dontDecode = false, bool silent = false, bool addOutDefault = true) { string output = ""; if (command != null && command.Length > 0) { using (Pipeline pipe = rs.Runspace.CreatePipeline()) { if (!dontDecode) { try { if (ProgramOptions.Base64) { command = Decoder.Base64Decode(command); } if (ProgramOptions.XorKey != 0) { command = Decoder.XorDecode(command, ProgramOptions.XorKey); } } catch (Exception e) { if (!silent) { Info($"[-] Could not decode command: {e.Message.ToString()}"); } } } else { if (!silent) { Info($"[?] Decided not to decode input command starting with: '{command.Substring(0, 30)}'"); Info($"[?] If you need that to be decoded as well, use --cmdencoded option."); } } pipe.Commands.AddScript(command); pipe.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output); if (addOutDefault) { pipe.Commands.Add("Out-default"); } try { pipe.Invoke(); command = ""; output = ((CustomPSHostUserInterface)host.UI).Output; ((CustomPSHostUserInterface)host.UI)._sb = new StringBuilder(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } return(output); }