Skip to content

Commit 47ec915

Browse files
committed
Fix return of script errors
1 parent 0335be2 commit 47ec915

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

PSScriptInvoker/PSScriptInvoker.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ protected override void OnStop()
131131
isServerStopped = true;
132132
server.Stop();
133133
server = null;
134-
//if (serverThread.IsAlive)
135-
// serverThread.Abort();
136134
runspacePool.Close();
137135
EventLog.WriteEntry(EVENT_LOG_SOURCE, "The service has been stopped.", EventLogEntryType.Information);
138136
}
@@ -228,19 +226,20 @@ private void handleRequest(object input)
228226
{
229227
scriptPath += segments[i].Replace("/", "") + "\\";
230228
}
231-
Dictionary<String, String> scriptOutput = executePowershellScript(scriptPath, scriptName, queryDict);
229+
string fullScriptPath = pathToScripts + scriptPath + scriptName + ".ps1";
230+
Dictionary<String, String> scriptOutput = executePowershellScript(fullScriptPath, queryDict);
232231

233232
// Get output variables
234-
string returnCode = "";
233+
string exitCode = "";
235234
string result = "";
236-
scriptOutput.TryGetValue("returnCode", out returnCode);
235+
scriptOutput.TryGetValue("exitCode", out exitCode);
237236
scriptOutput.TryGetValue("result", out result);
238237

239-
string msg = string.Format("Executed script was: {0}. Return code: {1}, output:\n{2}", request.Url.ToString(), returnCode, result);
238+
string msg = string.Format("Executed script was: {0}. Exit code: {1}, output:\n{2}", fullScriptPath, exitCode, result);
240239
Console.WriteLine(msg);
241240
EventLog.WriteEntry(EVENT_LOG_SOURCE, msg, EventLogEntryType.Information);
242241

243-
if (returnCode == "0")
242+
if (exitCode == "0")
244243
{
245244
if (string.IsNullOrEmpty(result))
246245
{
@@ -268,9 +267,8 @@ private void handleRequest(object input)
268267
/**
269268
* See here http://stackoverflow.com/a/527644
270269
*/
271-
private Dictionary<String, String> executePowershellScript(string scriptPath, string scriptName, Dictionary<String, String> inputs)
270+
private Dictionary<String, String> executePowershellScript(string fullScriptPath, Dictionary<String, String> inputs)
272271
{
273-
string fullScriptPath = pathToScripts + scriptPath + scriptName + ".ps1";
274272
foreach (string key in inputs.Keys)
275273
{
276274
string value = "";
@@ -284,7 +282,6 @@ private Dictionary<String, String> executePowershellScript(string scriptPath, st
284282

285283
Dictionary<String, String> output = new Dictionary<String, String>();
286284
Collection<PSObject> results = new Collection<PSObject>();
287-
IList errors = new ArrayList();
288285

289286
try
290287
{
@@ -293,19 +290,15 @@ private Dictionary<String, String> executePowershellScript(string scriptPath, st
293290
ps.RunspacePool = runspacePool;
294291
results = ps.Invoke();
295292

296-
if (results.Count > 0)
293+
if (ps.HadErrors)
297294
{
298-
output.Add("returnCode", "0");
295+
output.Add("exitCode", "1");
296+
results.Add(new PSObject((object)ps.Streams.Error));
299297
}
300298
else
301299
{
302-
output.Add("returnCode", "1");
303-
if (errors.Count > 0)
304-
{
305-
results.Add(new PSObject((object)errors[0]));
306-
}
300+
output.Add("exitCode", "0");
307301
}
308-
309302
}
310303
catch (ActionPreferenceStopException ex)
311304
{
@@ -320,13 +313,13 @@ private Dictionary<String, String> executePowershellScript(string scriptPath, st
320313
}
321314
EventLog.WriteEntry(EVENT_LOG_SOURCE, "Exception occurred in Powershell script '" + fullScriptPath + "':\n" + psEx.ToString(), EventLogEntryType.Error);
322315
results.Add(new PSObject((object)psEx.Message));
323-
output.Add("returnCode", "1");
316+
output.Add("exitCode", "1");
324317
}
325318
catch (Exception ex)
326319
{
327320
EventLog.WriteEntry(EVENT_LOG_SOURCE, "Unexpected exception while invoking Powershell script '" + fullScriptPath + "':\n" + ex.ToString(), EventLogEntryType.Error);
328321
results.Add(new PSObject((object)ex.Message));
329-
output.Add("returnCode", "1");
322+
output.Add("exitCode", "1");
330323
}
331324

332325
if (results.Count > 0)
@@ -401,7 +394,7 @@ private string readAppSetting(string key)
401394
try
402395
{
403396
var appSettings = ConfigurationManager.AppSettings;
404-
result = appSettings[key] ?? "Not Found";
397+
result = appSettings[key] ?? null;
405398
}
406399
catch (Exception ex)
407400
{

0 commit comments

Comments
 (0)