Last active
January 22, 2016 21:01
-
-
Save Jaykul/1bdc054aba13875a51e2 to your computer and use it in GitHub Desktop.
The wonderful world of CliXml output
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
// You can output any simple .Net Object that you like | |
using System; | |
using System.Collections.Generic; | |
using System.Management.Automation; | |
namespace PoshCode | |
{ | |
// A data object we're going to output | |
public class Vehicle | |
{ | |
public string Model { get; set; } | |
public string Make { get; set; } | |
public int Year { get; set; } | |
} | |
class Catalog | |
{ | |
static void Main() | |
{ | |
var output = new Vehicle[] | |
{ | |
new Vehicle { Model = "Cooper", Make = "Mini", Year = 2015 }, | |
new Vehicle { Model = "Mazda3", Make = "Mazda", Year = 2016 }, | |
}; | |
Console.Out.WriteLine("#< CLIXML"); | |
foreach(var item in output) { | |
Console.Out.WriteLine(PSSerializer.Serialize(item)); | |
} | |
} | |
} | |
} |
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
<#PS: #> # Note that Catalog outputs TEXT ONLY -- but in CliXML format with a magic header line ... | |
<#PS: #> add-type -Path .\Catalog.cs -OutputAssembly Catalog.exe -ReferencedAssemblies System.Xml | |
<#PS: #> .\Catalog.exe | |
#< CLIXML | |
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"> | |
<Obj RefId="0"> | |
<TN RefId="0"> | |
<T>PoshCode.Vehicle[]</T> | |
<T>System.Array</T> | |
<T>System.Object</T> | |
</TN> | |
<LST> | |
<Obj RefId="1"> | |
<TN RefId="1"> | |
<T>PoshCode.Vehicle</T> | |
<T>System.Object</T> | |
</TN> | |
<ToString>PoshCode.Vehicle</ToString> | |
<Props> | |
<S N="Model">Cooper</S> | |
<S N="Make">Mini</S> | |
<I32 N="Year">2015</I32> | |
</Props> | |
</Obj> | |
<Obj RefId="2"> | |
<TNRef RefId="1" /> | |
<ToString>PoshCode.Vehicle</ToString> | |
<Props> | |
<S N="Model">Mazda3</S> | |
<S N="Make">Mazda</S> | |
<I32 N="Year">2016</I32> | |
</Props> | |
</Obj> | |
</LST> | |
</Obj> | |
</Objs> | |
<#PS: #> # If you invoke it as part of a script, it turns into PowerShell objects: | |
<#PS: #> (.\Catalog.exe) | |
Model Make Year | |
----- ---- ---- | |
Cooper Mini 2015 | |
Mazda3 Mazda 2016 | |
<#PS: #> # Here's another example, taking advantage of this to write an executable that wraps a PowerShell script: | |
<#PS: #> add-type -Path .\Runner.cs -OutputAssembly Runner.exe -ReferencedAssemblies System.Xml | |
<#PS: #> .\Runner.exe . | Format-Table | |
Directory: C:\users\joel\Projects\PoshCodeRunner\PoshCodeRunner | |
Mode LastWriteTime Length Name | |
---- ------------- ------ ---- | |
d----- 12/17/2015 4:22 PM bin | |
d----- 12/17/2015 4:22 PM obj | |
d----- 12/17/2015 4:22 PM Properties | |
-a---- 12/17/2015 4:22 PM 189 App.config | |
-a---- 12/17/2015 4:27 PM 170 packages.config | |
-a---- 12/17/2015 4:54 PM 3097 PoshCodeRunner.csproj | |
-a---- 12/17/2015 5:07 PM 2385 Program.cs | |
-a---- 12/17/2015 5:07 PM 5632 Runner.exe | |
[468+]: .\Runner.exe . | Select -first 1 | |
Directory: C:\users\joel\Projects\PoshCodeRunner\PoshCodeRunner | |
Mode LastWriteTime Length Name | |
---- ------------- ------ ---- | |
d----- 12/17/2015 4:22 PM bin |
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
// Here's the code for my PowerShell script runner | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Management.Automation; | |
using System.Collections.ObjectModel; | |
using System.Reflection; | |
namespace PoshCode | |
{ | |
class Runner | |
{ | |
static void Main(string[] args) | |
{ | |
using (var ps = PowerShell.Create()) | |
{ | |
// use "AddScript" to add the contents of a script file to the end of the execution pipeline. | |
// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline. | |
ps.AddScript("param($path) Get-ChildItem $path"); | |
// use "AddParameter" to add a single parameter to the last command/script on the pipeline. | |
ps.AddParameter("path", args); | |
Console.Out.WriteLine("#< CLIXML"); | |
Console.Out.WriteLine(PSSerializer.Serialize(ps.Invoke())); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment