Tworzenie elementu InitialSessionState
Polecenia programu PowerShell są uruchamiane w przestrzeni uruchamiania. Aby hostować program PowerShell w aplikacji, należy utworzyć obiekt System.Management.Automation.Runspaces.Runspace. Z każdym obszarem runspace jest skojarzony obiekt System.Management.Automation.Runspaces.InitialSessionState. InitialSessionState określa właściwości przestrzeni uruchamiania, takie jak polecenia, zmienne i moduły, które są dostępne dla tego miejsca działania.
Tworzenie domyślnego stanu InitialSessionState
Metody CreateDefault i CreateDefault2 klasy InitialSessionState mogą służyć do tworzenia obiektu InitialSessionState. Metoda CreateDefault tworzy element InitialSessionState z załadowanym wszystkimi wbudowanymi poleceniami, natomiast metoda CreateDefault2 ładuje tylko polecenia wymagane do hostowania programu PowerShell (polecenia z modułu Microsoft.PowerShell.Core).
Jeśli chcesz dodatkowo ograniczyć polecenia dostępne w aplikacji hosta, musisz utworzyć ograniczony obszar działania. Aby uzyskać więcej informacji, zobacz Creating a constrained runspace(Tworzenie ograniczonego miejsca działania).
Poniższy kod pokazuje, jak utworzyć element InitialSessionState, przypisać go do przestrzeni uruchamiania, dodać polecenia do potoku w tym przestrzeni uruchamiania i wywołać polecenia. Aby uzyskać więcej informacji na temat dodawania i wywołania poleceń, zobacz Dodawanie i wywołania poleceń.
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.
}
Zobacz też
Opinia
https://aka.ms/ContentUserFeedback.
Już wkrótce: w ciągu 2024 r. będziemy stopniowo usuwać problemy z usługą GitHub jako mechanizm opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla