-
Notifications
You must be signed in to change notification settings - Fork 2
/
Measure.cs
267 lines (227 loc) · 7.21 KB
/
Measure.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using Microsoft.PowerShell;
using Rainmeter;
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Threading;
namespace PowershellRM
{
internal enum ScriptType
{
NotValid,
FileNoUpdate,
File,
Line
}
internal enum State
{
NotReady,
Ready
}
internal class Measure
{
internal API rmAPI;
internal APIProxy proxy;
internal Runspace runspace;
internal Command script;
internal double outputNumber;
internal string outputString;
internal ScriptType type = ScriptType.NotValid;
internal virtual void SetRmAPI() { }
internal virtual void Dispose() { }
internal virtual double Update() { return 0; }
internal virtual string GetString() { return null; }
internal virtual void Reload() { }
internal InitialSessionState CreateSessionState()
{
InitialSessionState initState = InitialSessionState.CreateDefault();
initState.ApartmentState = ApartmentState.MTA;
initState.ThreadOptions = PSThreadOptions.ReuseThread;
string executionPolicy = rmAPI.ReadString("ExecutionPolicy", null);
if (!string.IsNullOrEmpty(executionPolicy))
{
try
{
initState.ExecutionPolicy = (ExecutionPolicy)Enum.Parse(
typeof(ExecutionPolicy),
executionPolicy,
true
);
}
catch
{
rmAPI.LogF(
API.LogType.Warning,
"Unknown ExecutionPolicy: \"{0}\". Using \"Default\".",
executionPolicy
);
initState.ExecutionPolicy = ExecutionPolicy.Default;
}
}
return initState;
}
internal void PrepareEnvironment()
{
SetRmAPI();
runspace.SessionStateProxy
.Path.SetLocation(rmAPI.ReplaceVariables("#CURRENTPATH#"));
}
internal void Invoke()
{
if (type == ScriptType.NotValid ||
type == ScriptType.FileNoUpdate)
{
return;
}
using (Pipeline pipe = runspace.CreatePipeline())
{
SetRmAPI();
pipe.Commands.Add(script);
Collection<PSObject> outputCollection;
try
{
outputCollection = pipe.Invoke();
}
catch (Exception e)
{
rmAPI.Log(API.LogType.Error, e.ToString());
return;
}
object lastObject = null;
for (int i = outputCollection.Count - 1; i >= 0; i--)
{
if (outputCollection[i] != null)
{
lastObject = outputCollection[i].BaseObject;
break;
}
}
if (lastObject != null)
{
outputString = lastObject.ToString();
try
{
outputNumber = Convert.ToDouble(lastObject);
}
catch
{
outputNumber = 0;
}
}
}
}
internal void ExecuteBang(string args)
{
if (string.IsNullOrEmpty(args))
{
return;
}
try
{
using (Pipeline pipe = runspace.CreatePipeline())
{
pipe.Commands.AddScript(args);
pipe.Invoke();
}
}
catch (Exception e)
{
rmAPI.Log(API.LogType.Error, e.ToString());
}
}
internal Command GetCommandFromLine()
{
const string baseLine = "line";
string rawScript = "";
string command = rmAPI.ReadString(baseLine, null, false);
if (string.IsNullOrEmpty(command))
{
type = ScriptType.NotValid;
rmAPI.Log(API.LogType.Error, "Command not found.");
}
else
{
int lineNum = 1;
while (!string.IsNullOrEmpty(command))
{
rawScript += command + "\n";
++lineNum;
command = rmAPI.ReadString(baseLine + lineNum, null, false);
}
type = ScriptType.Line;
}
return new Command(rawScript, true);
}
internal virtual string SectionInvoke(string command)
{
if (runspace.RunspaceAvailability != RunspaceAvailability.Available)
{
return "";
}
using (Pipeline pipe = runspace.CreatePipeline())
{
pipe.Commands.AddScript(command);
try
{
var outputCollection = pipe.Invoke();
object lastObject = null;
for (int i = outputCollection.Count - 1; i >= 0; i--)
{
if (outputCollection[i] != null)
{
lastObject = outputCollection[i].BaseObject;
break;
}
}
if (lastObject != null)
{
return lastObject.ToString();
}
}
catch (Exception e)
{
rmAPI.Log(API.LogType.Error, e.Message);
}
}
return null;
}
internal virtual string SectionGetVariable(string variableName, string defaulValue)
{
if (runspace.RunspaceAvailability != RunspaceAvailability.Available)
{
return defaulValue;
}
try
{
object value = runspace.SessionStateProxy.GetVariable(variableName);
return value.ToString();
}
catch (Exception e)
{
rmAPI.Log(API.LogType.Error, e.Message);
}
return defaulValue;
}
internal virtual string SectionExpand(string input)
{
if (runspace.RunspaceAvailability != RunspaceAvailability.Available)
{
return input;
}
try
{
string value = runspace
.SessionStateProxy
.InvokeCommand
.ExpandString(input);
return value;
}
catch (Exception e)
{
rmAPI.Log(API.LogType.Error, e.Message);
}
return input;
}
}
}