101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
// <copyright file="Host01.cs" company="Microsoft Corporation">
|
|
// Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
|
// </copyright>
|
|
// 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;
|
|
|
|
/// <summary>
|
|
/// This class contains the Main entry point for this host application.
|
|
/// </summary>
|
|
internal class Host01
|
|
{
|
|
/// <summary>
|
|
/// Indicator to tell the host application that it should exit.
|
|
/// </summary>
|
|
private bool shouldExit;
|
|
|
|
/// <summary>
|
|
/// The exit code that the host application will use to exit.
|
|
/// </summary>
|
|
private int exitCode;
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the
|
|
/// host application should exit.
|
|
/// </summary>
|
|
public bool ShouldExit
|
|
{
|
|
get { return this.shouldExit; }
|
|
set { this.shouldExit = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the PSHost implementation will
|
|
/// use to tell the host application what code to use
|
|
/// when exiting.
|
|
/// </summary>
|
|
public int ExitCode
|
|
{
|
|
get { return this.exitCode; }
|
|
set { this.exitCode = value; }
|
|
}
|
|
|
|
/// <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">Parameter not used.</param>
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|