// // Copyright (c) 2009 Microsoft Corporation. All rights reserved. // // DISCLAIMER OF WARRANTY: The software is licensed “as-is.” You // bear the risk of using it. Microsoft gives no express warranties, // guarantees or conditions. You may have additional consumer rights // under your local laws which this agreement cannot change. To the extent // permitted under your local laws, Microsoft excludes the implied warranties // of merchantability, fitness for a particular purpose and non-infringement. namespace Microsoft.Samples.PowerShell.Host { using System; using System.Management.Automation; using System.Management.Automation.Runspaces; using PowerShell = System.Management.Automation.PowerShell; /// /// This class contains the Main entry point for this host application. /// internal class Host01 { /// /// Indicator to tell the host application that it should exit. /// private bool shouldExit; /// /// The exit code that the host application will use to exit. /// private int exitCode; /// /// Gets or sets a value indicating whether the /// host application should exit. /// public bool ShouldExit { get { return this.shouldExit; } set { this.shouldExit = value; } } /// /// Gets or sets the PSHost implementation will /// use to tell the host application what code to use /// when exiting. /// public int ExitCode { get { return this.exitCode; } set { this.exitCode = value; } } /// /// This sample uses the PowerShell class to execute /// a script that calls exit. The host application looks at /// this and prints out the result. /// /// Parameter not used. private 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... using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(myHost)) { myRunSpace.Open(); // Create a PowerShell to execute our commands... using (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(); } } }