Note
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier les répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de changer de répertoire.
Pour des raisons de performances ou de sécurité, vous pouvez restreindre les commandes Windows PowerShell disponibles pour votre application hôte. Pour ce faire, vous créez un vide system.Management.Automation.Runspaces.InitialSessionState en appelant la méthode System.Management.Automation.Runspaces.InitialSessionState.Create*, puis ajoutez uniquement les commandes que vous souhaitez disponibles.
L’utilisation d’un runspace qui charge uniquement les commandes que vous spécifiez offre des performances considérablement améliorées.
Vous utilisez les méthodes de la classe System.Management.Automation.Runspaces.SessionStateCmdletEntry pour définir des applets de commande pour l’état de session initial.
Vous pouvez également rendre les commandes privées. Les commandes privées peuvent être utilisées par l’application hôte, mais pas par les utilisateurs de l’application.
Ajout de commandes à un runspace vide
L’exemple suivant montre comment créer un InitialSessionState vide et y ajouter des commandes.
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();
}
}
}
Création de commandes privées
Vous pouvez également rendre une commande privée en définissant sa propriété System.Management.Automation.CommandInfo.Visibility sur System.Management.Automation.SessionStateEntryVisibilityprivé. L’application hôte et d’autres commandes peuvent appeler cette commande, mais l’utilisateur de l’application ne peut pas. Dans l’exemple suivant, la commande Get-ChildItem est privée.
defaultSessionState = InitialSessionState.CreateDefault();
commandIndex = GetIndexOfEntry(defaultSessionState.Commands, "Get-ChildItem");
defaultSessionState.Commands[commandIndex].Visibility = SessionStateEntryVisibility.Private;
this.runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
this.runspace.Open();
Voir aussi
Création d’un InitialSessionState