From b59f16a425f768f7b3e1bdd6138546dcafea97d5 Mon Sep 17 00:00:00 2001 From: Frank Skare Date: Mon, 9 Sep 2019 05:56:08 +0200 Subject: [PATCH] improved PowerShell scrip host --- mpv.net/Scripting/PowerShellScript.cs | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/mpv.net/Scripting/PowerShellScript.cs b/mpv.net/Scripting/PowerShellScript.cs index 1988582..be7fd9a 100644 --- a/mpv.net/Scripting/PowerShellScript.cs +++ b/mpv.net/Scripting/PowerShellScript.cs @@ -9,7 +9,7 @@ namespace mpvnet { public class PowerShellScript { - public static object Execute(string filepath, params string[] parameters) + public static void Execute(string filepath, params string[] parameters) { using (Runspace runspace = RunspaceFactory.CreateRunspace()) { @@ -29,29 +29,30 @@ namespace mpvnet foreach (string i in parameters) pipeline.Commands[1].Parameters.Add(null, i); - try - { - var output = new PowerShellOutput(); - output.ModuleName = Path.GetFileName(filepath); + PowerShellOutput output = new PowerShellOutput(); + output.ModuleName = Path.GetFileName(filepath); - pipeline.Output.DataReady += output.Output_DataReady; - pipeline.Error.DataReady += output.Error_DataReady; + pipeline.Output.DataReady += output.Output_DataReady; + pipeline.Error.DataReady += output.Error_DataReady; - runspace.SessionStateProxy.SetVariable("Output", output); + runspace.SessionStateProxy.SetVariable("Output", output); - var ret = pipeline.Invoke(); - if (ret.Count > 0) return ret[0]; - - pipeline.Output.DataReady -= output.Output_DataReady; - pipeline.Error.DataReady -= output.Error_DataReady; + try { + pipeline.Invoke(); } - catch (Exception e) - { + catch (RuntimeException e) { + Msg.ShowError("PowerShell Exception", e.Message + "\n\n" + + e.ErrorRecord.ScriptStackTrace.Replace(" , ", "") + + "\n\n" + Path.GetFileName(filepath)); + } + catch (Exception e) { Msg.ShowException(e); } + + pipeline.Output.DataReady -= output.Output_DataReady; + pipeline.Error.DataReady -= output.Error_DataReady; } } - return null; } public static void Init(string filepath)