public static int InvokePowerShell(string path, string param) { int code = ResultCode.RESULT_ERROR; Host01 me = new Host01(); MyHost myHost = new MyHost(me); using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost)) { myRunSpace.Open(); using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = myRunSpace; string script = File.ReadAllText(path); powershell.AddScript(script); try { powershell.Invoke(); code = me.ExitCode; } catch { code = ResultCode.RESULT_ERROR; } } myRunSpace.Close(); } return(code); }
/// <summary> /// Initializes a new instance of the MyHost class. Keep /// a reference to the host application object so that it /// can be informed of when to exit. /// </summary> /// <param name="program"> /// A reference to the host application object. /// </param> public MyHost(Host01 program) { this.program = program; }