Megjegyzés
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhat bejelentkezni vagy módosítani a címtárat.
Az oldalhoz való hozzáféréshez engedély szükséges. Megpróbálhatja módosítani a címtárat.
Teljesítmény- vagy biztonsági okokból érdemes lehet korlátozni a gazdaalkalmazás számára elérhető Windows PowerShell-parancsokat. Ehhez hozzon létre egy üres System.Management.Automation.Runspaces.InitialSessionState a System.Management.Automation.Runspaces.InitialSessionState.Create* metódus meghívásával, majd adja hozzá csak az elérhető parancsokat.
Ha olyan futtatóteret használ, amely csak a megadott parancsokat tölti be, jelentősen jobb teljesítményt nyújt.
A System.Management.Automation.Runspaces.SessionStateCmdletEntry osztály metódusaival definiálhatja a kezdeti munkamenetállapot parancsmagjait.
A parancsokat privátsá is teheti. A privát parancsokat a gazdaalkalmazás használhatja, az alkalmazás felhasználói azonban nem.
Parancsok hozzáadása üres runspace-hez
Az alábbi példa bemutatja, hogyan hozhat létre üres InitialSessionState-t, és adhat hozzá parancsokat.
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();
}
}
}
Parancsok privátsá tétele
A parancsokat privátsá is teheti, ha a System.Management.Automation.CommandInfo.Visibility tulajdonságot System.Management.Automation.SessionStateEntryVisibilityPrivate. A gazdaalkalmazás és más parancsok meghívhatják ezt a parancsot, de az alkalmazás felhasználója nem. Az alábbi példában a Get-ChildItem parancs privát.
defaultSessionState = InitialSessionState.CreateDefault();
commandIndex = GetIndexOfEntry(defaultSessionState.Commands, "Get-ChildItem");
defaultSessionState.Commands[commandIndex].Visibility = SessionStateEntryVisibility.Private;
this.runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
this.runspace.Open();
Lásd még:
InitialSessionState létrehozása