Tworzenie ograniczonego obszaru działania
Ze względu na wydajność lub bezpieczeństwo można ograniczyć liczbę poleceń Windows PowerShell dostępnych dla aplikacji hosta. W tym celu należy utworzyć pustą metodę System.Management.Automation.Runspaces.Initialsessionstate, wywołując metodę System.Management.Automation.Runspaces.Initialsessionstate.Create*, a następnie dodać tylko te polecenia, które mają być dostępne.
Użycie przestrzeni działania, która ładuje tylko określone polecenia, zapewnia znacznie lepszą wydajność.
Metody klasy System.Management.Automation.Runspaces.Sessionstatecmdletentry są stosowane do definiowania polecenia cmdlet dla początkowego stanu sesji.
Można również ustawić polecenia jako prywatne. Z poleceń prywatnych może korzystać aplikacja hosta, ale nie użytkownicy aplikacji.
Dodawanie poleceń do pustego miejsca działania
W poniższym przykładzie pokazano, jak utworzyć pusty element InitialSessionState i dodać do niego polecenia.
namespace Microsoft.Samples.PowerShell.Runspaces
{
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands;
using PowerShell = System.Management.Automation.PowerShell;
/// <summary>
/// This class contains the Main entry point for the application.
/// </summary>
internal class Runspace10b
{
/// <summary>
/// This sample shows how to create an empty initial session state,
/// how to add commands to the session state, and then how to create a
/// runspace that has only those two commands. A PowerShell object
/// is used to run the Get-Command cmdlet to show that only two commands
/// are available.
/// </summary>
/// <param name="args">Parameter not used.</param>
private static void Main(string[] args)
{
// Create an empty InitialSessionState and then add two commands.
InitialSessionState iss = InitialSessionState.Create();
// Add the Get-Process and Get-Command cmdlets to the session state.
SessionStateCmdletEntry ssce1 = new SessionStateCmdletEntry(
"get-process",
typeof(GetProcessCommand),
null);
iss.Commands.Add(ssce1);
SessionStateCmdletEntry ssce2 = new SessionStateCmdletEntry(
"get-command",
typeof(GetCommandCommand),
null);
iss.Commands.Add(ssce2);
// Create a runspace.
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
{
myRunSpace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = myRunSpace;
// Create a pipeline with the Get-Command command.
powershell.AddCommand("get-command");
Collection<PSObject> results = powershell.Invoke();
Console.WriteLine("Verb Noun");
Console.WriteLine("----------------------------");
// Display each result object.
foreach (PSObject result in results)
{
Console.WriteLine(
"{0,-20} {1}",
result.Members["verb"].Value,
result.Members["Noun"].Value);
}
}
// Close the runspace and release any resources.
myRunSpace.Close();
}
System.Console.WriteLine("Hit any key to exit...");
System.Console.ReadKey();
}
}
}
Tworzenie prywatnych poleceń
Można również ustawić polecenie jako prywatne, ustawiając właściwość System.Management.Automation.Commandinfo.Visibility na Wartość System.Management.Automation.SessionStateEntryVisibility Private. Aplikacja hosta i inne polecenia mogą wywołać to polecenie, ale użytkownik aplikacji nie może. W poniższym przykładzie polecenie Get-ChildItem jest prywatne.
defaultSessionState = InitialSessionState.CreateDefault();
commandIndex = GetIndexOfEntry(defaultSessionState.Commands, "get-childitem");
defaultSessionState.Commands[commandIndex].Visibility = SessionStateEntryVisibility.Private;
this.runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
this.runspace.Open();
Zobacz też
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania 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