-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowershell-command-runner.cake
66 lines (57 loc) · 1.91 KB
/
powershell-command-runner.cake
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
public class PowershellCommandRunner
{
protected ICakeContext context;
protected bool globalExceptionOnError;
public PowershellCommandRunner(ICakeContext context, bool globalExceptionOnError = true)
{
this.context = context;
this.globalExceptionOnError = globalExceptionOnError;
}
public T Run<T>(
string command,
Action<ProcessArgumentBuilder> argumentBuilder,
Nullable<bool> exceptionOnError = null,
bool logOutput = true)
{
var output = RunCommand(
command,
argumentBuilder,
exceptionOnError,
logOutput);
return context.DeserializeJson<T>(output);
}
public void Run(
string command,
Action<ProcessArgumentBuilder> argumentBuilder,
Nullable<bool> exceptionOnError = null,
bool logOutput = true)
{
RunCommand(
command,
argumentBuilder,
exceptionOnError,
logOutput);
}
public string RunCommand(
string command,
Action<ProcessArgumentBuilder> argumentBuilder,
Nullable<bool> commandExceptionOnError = null,
bool logOutput = true)
{
var exceptionOnError = commandExceptionOnError ?? this.globalExceptionOnError;
if (!exceptionOnError) {
this.context.Information("ExceptionOnScriptError - OFF");
}
var psSettings = new PowershellSettings
{
ExceptionOnScriptError = exceptionOnError,
OutputToAppConsole = logOutput,
LogOutput = logOutput
}.WithArguments(argumentBuilder);
var outputLines = this.context.StartPowershellScript(command, psSettings)
.Where(p => p.BaseObject is String)
.Select(p => p.BaseObject)
.Cast<string>();
return string.Join(Environment.NewLine, outputLines);
}
}