Skip to content

Commit 0548349

Browse files
committed
Modified previous ConstrainedLanguage changes to support spawning runspace based on InitialSessionState instead of just the LanguageMode of the InitialSessionState.
1 parent fd0e360 commit 0548349

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Microsoft.PowerShell.EditorServices.Services
2828
using System.Management.Automation;
2929
using Microsoft.PowerShell.EditorServices.Handlers;
3030
using Microsoft.PowerShell.EditorServices.Hosting;
31-
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
31+
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
3232

3333
/// <summary>
3434
/// Manages the lifetime and usage of a PowerShell session.
@@ -222,8 +222,8 @@ public static PowerShellContextService Create(
222222
hostStartupInfo.InitialSessionState.ImportPSModule((IEnumerable<Commands.ModuleSpecification>)hostStartupInfo.AdditionalModules);
223223
hostStartupInfo.InitialSessionState.ImportPSModule(new string[] { s_commandsModulePath });
224224
}
225-
Runspace runspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
226-
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, runspace, true, hostUserInterface);
225+
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
226+
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
227227
// TODO: This can be moved to the point after the $psEditor object
228228
// gets initialized when that is done earlier than LanguageServer.Initialize
229229
if (hostStartupInfo.InitialSessionState.LanguageMode == PSLanguageMode.FullLanguage)
@@ -270,15 +270,50 @@ public static Runspace CreateRunspace(
270270
powerShellContext.ConsoleReader = hostUserInterface;
271271
return CreateRunspace(psHost, hostDetails.InitialSessionState);
272272
}
273-
273+
private static InitialSessionState GetUsefulConstrainedISS()
274+
{
275+
InitialSessionState iss = InitialSessionState.Create("Microsoft.PowerShell.Core");
276+
iss.LanguageMode = PSLanguageMode.ConstrainedLanguage;
277+
iss.ImportPSModule(new string[] { "Microsoft.Powershell.Utility", "Microsoft.Powershell.Core", "Microsoft.PowerShell.Security" });
278+
279+
iss.Commands.Add(new SessionStateCmdletEntry("Get-Command", typeof(GetCommandCommand), null));
280+
iss.Commands.Add(new SessionStateCmdletEntry("Get-ChildItem", typeof(PSCommand), null)); //
281+
iss.Commands.Add(new SessionStateCmdletEntry("Export-ModuleMember", typeof(ExportModuleMemberCommand), null));
282+
iss.Commands.Add(new SessionStateCmdletEntry("Where-Object", typeof(WhereObjectCommand), null));
283+
iss.Commands.Add(new SessionStateCmdletEntry("Select-Object", typeof(PSCommand), null));
284+
iss.Commands.Add(new SessionStateCmdletEntry("Set-Variable", typeof(PSCommand), null));
285+
iss.Commands.Add(new SessionStateCmdletEntry("ForEach-Object", typeof(ForEachObjectCommand), null));
286+
iss.Commands.Add(new SessionStateCmdletEntry("Format-List", typeof(PSCommand), null));
287+
iss.Commands.Add(new SessionStateCmdletEntry("Format-Table", typeof(PSCommand), null));
288+
iss.Commands.Add(new SessionStateCmdletEntry("Set-ExecutionPolicy", typeof(PSCommand), null));
289+
iss.Commands.Add(new SessionStateCmdletEntry("Format-Hex", typeof(PSCommand), null));
290+
//iss.Commands.Add(new SessionStateCmdletEntry("Out-Default", typeof(OutDefaultCommand), null));
291+
iss.Commands.Add(new SessionStateCmdletEntry("Microsoft.PowerShell.Core\\Out-Default", typeof(OutDefaultCommand), null) { Visibility = SessionStateEntryVisibility.Public });
292+
iss.Commands.Add(new SessionStateCmdletEntry("Out-Host", typeof(OutHostCommand), null));
293+
iss.Commands.Add(new SessionStateCmdletEntry("Import-Module", typeof(ImportModuleCommand), null));
294+
iss.Commands.Add(new SessionStateCmdletEntry("Start-EditorServices", typeof(PSCommand), null));
295+
296+
return iss;
297+
}
274298
/// <summary>
275299
///
276300
/// </summary>
277301
/// <param name="psHost">The PSHost that will be used for this Runspace.</param>
278302
/// <param name="initialSessionState">The initialSessionState inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same initialSessionState including allowed modules, cmdlets and language mode.</param>
279303
/// <returns></returns>
280-
internal static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
304+
public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
281305
{
306+
if (initialSessionState == null)
307+
{
308+
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1")
309+
{
310+
initialSessionState = InitialSessionState.CreateDefault();
311+
}
312+
else
313+
{
314+
initialSessionState = InitialSessionState.CreateDefault2();
315+
}
316+
}
282317
Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState);
283318

284319
// Windows PowerShell must be hosted in STA mode

0 commit comments

Comments
 (0)