improved PowerShell scrip host

This commit is contained in:
Frank Skare
2019-09-09 05:56:08 +02:00
parent 46b110d93b
commit b59f16a425

View File

@@ -9,7 +9,7 @@ namespace mpvnet
{ {
public class PowerShellScript 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()) using (Runspace runspace = RunspaceFactory.CreateRunspace())
{ {
@@ -29,9 +29,7 @@ namespace mpvnet
foreach (string i in parameters) foreach (string i in parameters)
pipeline.Commands[1].Parameters.Add(null, i); pipeline.Commands[1].Parameters.Add(null, i);
try PowerShellOutput output = new PowerShellOutput();
{
var output = new PowerShellOutput();
output.ModuleName = Path.GetFileName(filepath); output.ModuleName = Path.GetFileName(filepath);
pipeline.Output.DataReady += output.Output_DataReady; pipeline.Output.DataReady += output.Output_DataReady;
@@ -39,20 +37,23 @@ namespace mpvnet
runspace.SessionStateProxy.SetVariable("Output", output); runspace.SessionStateProxy.SetVariable("Output", output);
var ret = pipeline.Invoke(); try {
if (ret.Count > 0) return ret[0]; pipeline.Invoke();
}
catch (RuntimeException e) {
Msg.ShowError("PowerShell Exception", e.Message + "\n\n" +
e.ErrorRecord.ScriptStackTrace.Replace(" <ScriptBlock>, <No file>", "") +
"\n\n" + Path.GetFileName(filepath));
}
catch (Exception e) {
Msg.ShowException(e);
}
pipeline.Output.DataReady -= output.Output_DataReady; pipeline.Output.DataReady -= output.Output_DataReady;
pipeline.Error.DataReady -= output.Error_DataReady; pipeline.Error.DataReady -= output.Error_DataReady;
} }
catch (Exception e)
{
Msg.ShowException(e);
} }
} }
}
return null;
}
public static void Init(string filepath) public static void Init(string filepath)
{ {