Creating an InitialSessionState
Windows PowerShell commands run in a runspace. To host Windows PowerShell in your application, you must create a Runspace object. Every runspace has an InitialSessionState object associated with it. The InitialSessionState specifies characteristics of the runspace, such as which commands, variables, and modules are available for that runspace.
The CreateDefaultand M:System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2 methods can be used to create InitialSessionState objects. CreateDefault creates an InitialSessionState with all of the built-in commands loaded, while M:System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2 loads only the commands required to host Windows PowerShell (the commands from the Microsoft.PowerShell.Core module.
If you want to further limit the commands available in your host application you need to create a constrained runspace. For information, see Creating a constrained runspace.
The following code shows how to create an InitialSessionState, assign it to a runspace, add commands to the pipeline in that runspace, and invoke the commands. For more information about adding and invoking commands, see Adding and invoking commands.
namespace SampleHost
{
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
class HostP4b
{
static void Main(string[] args)
{
// Call the InitailSessionState.CreateDefault method to create
// an empty InitialSessionState object, and then add the
// elements that will be available when the runspace is opened.
InitialSessionState iss = InitialSessionState.CreateDefault();
SessionStateVariableEntry var1 = new
SessionStateVariableEntry("test1",
"MyVar1",
"Initial session state MyVar1 test");
iss.Variables.Add(var1);
SessionStateVariableEntry var2 = new
SessionStateVariableEntry("test2",
"MyVar2",
"Initial session state MyVar2 test");
iss.Variables.Add(var2);
// Call the RunspaceFactory.CreateRunspace(InitialSessionState)
// method to create the runspace where the pipeline is run.
Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();
// Call the PowerShell.Create() method to create the PowerShell
// object,and then specify the runspace and commands to the pipeline.
// and create the command pipeline.
PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Get-Variable");
ps.AddArgument("test*");
Console.WriteLine("Variable Value");
Console.WriteLine("--------------------------");
// Call the PowerShell.Invoke() method to run
// the pipeline synchronously.
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine("{0,-20}{1}",
result.Members["Name"].Value,
result.Members["Value"].Value);
} // End foreach.
// Close the runspace to free resources.
rs.Close();
} // End Main.
} // End SampleHost.
}