Een InitialSessionState maken
PowerShell-opdrachten worden uitgevoerd in een runspace. Als u PowerShell in uw toepassing wilt hosten, moet u een System.Management.Automation.Runspaces.Runspace-object maken. Aan elke runspace is een System.Management.Automation.Runspaces.InitialSessionState-object gekoppeld. InitialSessionState specificeert kenmerken van de runspace, zoals welke opdrachten, variabelen en modules beschikbaar zijn voor die runspace.
Een standaard InitialSessionState maken
De methoden CreateDefault en CreateDefault2 van de klasse InitialSessionState kunnen worden gebruikt om een InitialSessionState-object te maken. Met de methode CreateDefault wordt een InitialSessionState gemaakt waarbij alle ingebouwde opdrachten zijn geladen, terwijl de methode CreateDefault2 alleen de opdrachten laadt die nodig zijn om PowerShell te hosten (de opdrachten van de Module Microsoft.PowerShell.Core).
Als u de opdrachten die beschikbaar zijn in uw hosttoepassing verder wilt beperken, moet u een beperkte runspace maken. Zie Creating a constrained runspace (Een beperkte runspace maken) voor meer informatie.
De volgende code laat zien hoe u een InitialSessionState maakt, deze toewijst aan een runspace, opdrachten toevoegt aan de pijplijn in die runspace en de opdrachten aanroept. Zie Opdrachten toevoegen en aanroepen voor meer informatie over het toevoegen en aanroepen van opdrachten.
namespace SampleHost
{
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
class HostP4b
{
static void Main(string[] args)
{
// Call the InitialSessionState.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.
}
Zie ook
Feedback
Feedback verzenden en weergeven voor