forked from flcdrg/PowerShellWixExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerShellTask.cs
More file actions
142 lines (120 loc) · 4.8 KB
/
PowerShellTask.cs
File metadata and controls
142 lines (120 loc) · 4.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
namespace PowerShellActions
{
internal class PowerShellTask : IDisposable, IExitCode
{
private readonly Session _session;
/// <summary>
/// The context that the Windows PowerShell script will run under.
/// </summary>
private Pipeline _pipeline;
internal PowerShellTask(string script, Session session)
{
_session = session;
Runspace runspace = RunspaceFactory.CreateRunspace(new WixHost(session, this));
_pipeline = runspace.CreatePipeline();
_pipeline.Commands.AddScript(script);
_pipeline.Runspace.Open();
_pipeline.Runspace.SessionStateProxy.SetVariable("session", session);
}
internal PowerShellTask(string file, string arguments, Session session)
{
_session = session;
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(new WixHost(session, this), runspaceConfiguration);
runspace.Open();
var scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy RemoteSigned -Scope Process");
_pipeline = runspace.CreatePipeline();
// http://stackoverflow.com/a/530418/25702
_pipeline.Commands.AddScript(string.Format("& '{0}' {1}", file, arguments));
_pipeline.Runspace.SessionStateProxy.SetVariable("session", session);
}
public int ExitCode { get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Execute Script, return false if any errors
/// </summary>
/// <returns></returns>
public bool Execute()
{
var results = _pipeline.Invoke();
using (var record = new Record(0))
{
record[0] = string.Format("Exit code {0}", ExitCode);
_session.Message(InstallMessage.Info, record);
if (results?.Any() ?? false)
{
_session.Log("Output");
foreach (var r in results)
{
if (r?.BaseObject != null)
{
_session.Log("\t" + r?.BaseObject?.ToString() ?? "");
}
}
}
var errors = Errors();
if (errors != null)
{
_session.Log("Non-terminating errors");
record[0] = errors;
_session.Message(InstallMessage.Error, record);
}
// Using .Error instead of .HadErrors to support any PS version.
return (((_pipeline?.Error?.Count ?? 0) == 0) && (errors == null) && (ExitCode == 0));
}
}
public string Errors()
{
// check for errors (non-terminating)
if ((_pipeline?.Error?.Count ?? 0) > 0)
{
var builder = new StringBuilder();
// iterate over Error PipeLine until end
while (!_pipeline.Error.EndOfPipeline)
{
// read one PSObject off the pipeline
var value = _pipeline.Error.Read() as PSObject;
if (value != null)
{
// get the ErrorRecord
var r = value.BaseObject as ErrorRecord;
if (r != null)
{
// build whatever kind of message you want
builder.AppendLine(r.InvocationInfo?.MyCommand?.Name ?? "" + " : " + r.Exception.Message);
builder.AppendLine(r.InvocationInfo?.PositionMessage ?? "");
builder.AppendLine(string.Format("+ CategoryInfo: {0}", r.CategoryInfo));
builder.AppendLine(
string.Format("+ FullyQualifiedErrorId: {0}", r.FullyQualifiedErrorId ?? ""));
}
}
}
return builder.ToString();
}
return null;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_pipeline.Runspace != null)
{
_pipeline.Runspace.Dispose();
_pipeline = null;
}
}
}
}
}