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
|
using PKISharp.WACS.Services;
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace PKISharp.WACS.Clients
{
public class ScriptClient
{
protected ILogService _log;
protected ISettingsService _settings;
public ScriptClient(ILogService logService, ISettingsService settings)
{
_log = logService;
_settings = settings;
}
public async Task RunScript(string script, string parameters)
{
if (!string.IsNullOrWhiteSpace(script))
{
var actualScript = Environment.ExpandEnvironmentVariables(script);
var actualParameters = parameters;
if (actualScript.EndsWith(".ps1"))
{
actualScript = "powershell.exe";
actualParameters = $"-windowstyle hidden -noninteractive -executionpolicy bypass .'{script}' {parameters.Replace("\"", "\"\"\"")}";
}
var PSI = new ProcessStartInfo(actualScript)
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
if (!string.IsNullOrWhiteSpace(actualParameters))
{
_log.Information(LogType.All, "Script {script} starting with parameters {parameters}", script, parameters);
PSI.Arguments = actualParameters;
}
else
{
_log.Information(LogType.All, "Script {script} starting", script);
}
try
{
using var process = new Process { StartInfo = PSI };
var output = new StringBuilder();
process.OutputDataReceived += (s, e) =>
{
if (e.Data != null)
{
output.AppendLine(e.Data);
_log.Verbose(e.Data);
}
else
{
_log.Verbose("Process output without data received");
}
};
process.ErrorDataReceived += (s, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Data) && !string.Equals(e.Data, "null"))
{
output.AppendLine($"Error: {e.Data}");
_log.Error("Script error: {0}", e.Data);
}
else
{
_log.Verbose("Process error without data received");
}
};
var exited = false;
process.EnableRaisingEvents = true;
process.Exited += (s, e) =>
{
_log.Information(LogType.Event | LogType.Disk, output.ToString());
exited = true;
if (process.ExitCode != 0)
{
_log.Error("Script finished with exit code {code}", process.ExitCode);
}
else
{
_log.Information("Script finished");
}
};
if (process.Start())
{
_log.Debug("Process launched: {actualScript} (ID: {Id})", actualScript, process.Id);
}
else
{
throw new Exception("Process.Start() returned false");
}
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.StandardInput.Close(); // Helps end the process
var totalWait = 0;
var interval = 2000;
while (!exited && totalWait < _settings.Script.Timeout * 1000)
{
await Task.Delay(interval);
totalWait += interval;
_log.Verbose("Waiting for process to finish...");
}
if (!exited)
{
_log.Error($"Script execution timed out after {_settings.Script.Timeout} seconds, trying to kill");
try
{
process.Kill();
}
catch (Exception ex)
{
_log.Error(ex, "Killing process {Id} failed", process.Id);
}
}
}
catch (Exception ex)
{
_log.Error(ex, "Script is unable to start");
}
}
else
{
_log.Warning("No script configured.");
}
}
}
}
|