/// <summary> /// This sample uses the RunspaceInvoke class to execute /// a script that calls exit. The host application looks at /// this and prints out the result. /// </summary> /// <param name="args">Unused</param> static void Main(string[] args) { // Create an instance of this class so that the engine will have // access to the ShouldExit and ExitCode parameters. Host01 me = new Host01(); // Now create the host instance to use MyHost myHost = new MyHost(me); // Pass this in when creating the runspace and invoker... Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost); myRunSpace.Open(); RunspaceInvoke invoker = new RunspaceInvoke(myRunSpace); // Now use the runspace invoker to execute the script "exit (2+2)" string script = "exit (2+2)"; invoker.Invoke(script); // Check the flags and see if they were set propertly... Console.WriteLine( "ShouldExit={0} (should be True); ExitCode={1} (should be 4)", me.ShouldExit, me.ExitCode); // close the runspace... myRunSpace.Close(); Console.WriteLine("Hit any key to exit..."); Console.ReadKey(); }
/// <summary> /// This sample uses the PowerShell class to execute /// a script that calls exit. The host application looks at /// this and prints out the result. /// </summary> /// <param name="args">Unused</param> static void Main(string[] args) { // Create an instance of this class so that the engine will have // access to the ShouldExit and ExitCode parameters. Host01 me = new Host01(); // Now create the host instance to use MyHost myHost = new MyHost(me); // Pass this in when creating the runspace and invoker... Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost); myRunSpace.Open(); // Create a PowerShell to execute our commands... PowerShell powershell = PowerShell.Create(); powershell.Runspace = myRunSpace; // Now use the runspace invoker to execute the script "exit (2+2)" string script = "exit (2+2)"; powershell.AddScript(script); powershell.Invoke(script); // Check the flags and see if they were set propertly... Console.WriteLine( "ShouldExit={0} (should be True); ExitCode={1} (should be 4)", me.ShouldExit, me.ExitCode); // close the runspace... myRunSpace.Close(); Console.WriteLine("Hit any key to exit..."); Console.ReadKey(); }
/// <summary> /// Construct an instance of this PSHost implementation. Keep /// a reference to the hosting application object so 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; }