-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (47 loc) · 1.83 KB
/
Copy pathProgram.cs
File metadata and controls
54 lines (47 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Management.Automation;
using System.Management.Automation.Runspaces;
// Retrieve the user script from manifest resources
string scriptBody = string.Empty;
using (Stream? resourceStream = typeof(Program).Assembly.GetManifestResourceStream("PSToExe.Script.ps1"))
{
if (resourceStream is null)
{
Console.WriteLine("Unable to load embedded Script.ps1.");
Environment.Exit(1);
}
using StreamReader reader = new (resourceStream, System.Text.Encoding.UTF8);
scriptBody = reader.ReadToEnd();
}
var ps = PowerShell.Create();
// Prepare to capture the various data streams.
ps.Streams.Verbose.DataAdded += ConsumePSDataStream;
ps.Streams.Debug.DataAdded += ConsumePSDataStream;
ps.Streams.Information.DataAdded += ConsumePSDataStream;
ps.Streams.Warning.DataAdded += ConsumePSDataStream;
ps.Streams.Error.DataAdded += ConsumePSDataStream;
var outputCollection = new PSDataCollection<PSObject>();
outputCollection.DataAdded += ConsumePSDataStream;
// Run the script
ps.AddScript(scriptBody).AddCommand("Out-String");
ps.Commands.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
ps.Invoke(null, outputCollection);
// callback for PowerShell data stream events
static void ConsumePSDataStream(object? sender, DataAddedEventArgs evtArgs)
{
// early out if nulls
if (sender is null || evtArgs is null)
return;
// Attempt to grab the most meaningful object to output
var data = ((System.Collections.IList)sender)[evtArgs.Index];
var output = data switch
{
VerboseRecord vr => vr.Message,
InformationRecord ir => ir.MessageData,
WarningRecord wr => wr.Message,
DebugRecord dr => dr.Message,
ErrorRecord er => er.ToString(),
_ => data,
};
// It's always Console.WriteLine!
Console.WriteLine(output);
}