Skip to content

Commit d9fef9c

Browse files
change some to AddScript useLocalScope
1 parent 1ff55ec commit d9fef9c

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private string GetPSOutputEncoding()
332332
{
333333
using (var pwsh = SMA.PowerShell.Create())
334334
{
335-
return pwsh.AddScript("$OutputEncoding.EncodingName").Invoke<string>()[0];
335+
return pwsh.AddScript("$OutputEncoding.EncodingName", useLocalScope: true).Invoke<string>()[0];
336336
}
337337
}
338338

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
597597
// cancelled prompt when it's called again.
598598
if (executionOptions.AddToHistory)
599599
{
600-
this.PromptContext.AddToHistory(psCommand.Commands[0].CommandText);
600+
this.PromptContext.AddToHistory(executionOptions.InputString ?? psCommand.Commands[0].CommandText);
601601
}
602602

603603
bool hadErrors = false;
@@ -686,7 +686,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
686686
if (executionOptions.WriteInputToHost)
687687
{
688688
this.WriteOutput(
689-
psCommand.Commands[0].CommandText,
689+
executionOptions.InputString ?? psCommand.Commands[0].CommandText,
690690
includeNewLine: true);
691691
}
692692

@@ -1161,7 +1161,7 @@ internal static TResult ExecuteScriptAndGetItem<TResult>(string scriptToExecute,
11611161
using (PowerShell pwsh = PowerShell.Create())
11621162
{
11631163
pwsh.Runspace = runspace;
1164-
IEnumerable<TResult> results = pwsh.AddScript(scriptToExecute).Invoke<TResult>();
1164+
IEnumerable<TResult> results = pwsh.AddScript(scriptToExecute, useLocalScope: true).Invoke<TResult>();
11651165
return results.DefaultIfEmpty(defaultValue).First();
11661166
}
11671167
}

src/PowerShellEditorServices/Services/PowerShellContext/Session/Capabilities/DscBreakpointCapability.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,17 @@ public static DscBreakpointCapability CheckForCapability(
121121
runspaceDetails.AddCapability(capability);
122122

123123
powerShell.Commands.Clear();
124-
powerShell.AddScript("Write-Host \"Gathering DSC resource paths, this may take a while...\"");
125-
powerShell.Invoke();
124+
powerShell
125+
.AddCommand("Microsoft.PowerShell.Utility\\Write-Host")
126+
.AddArgument("Gathering DSC resource paths, this may take a while...")
127+
.Invoke();
126128

127129
// Get the list of DSC resource paths
128130
powerShell.Commands.Clear();
129-
powerShell.AddCommand("Get-DscResource");
130-
powerShell.AddCommand("Select-Object");
131-
powerShell.AddParameter("ExpandProperty", "ParentPath");
131+
powerShell
132+
.AddCommand("Get-DscResource")
133+
.AddCommand("Select-Object")
134+
.AddParameter("ExpandProperty", "ParentPath");
132135

133136
Collection<PSObject> resourcePaths = null;
134137

src/PowerShellEditorServices/Services/PowerShellContext/Session/ExecutionOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ internal class ExecutionOptions
4646
/// </summary>
4747
public bool WriteInputToHost { get; set; }
4848

49+
/// <summary>
50+
/// If this is set, we will use this string for history and writing to the host
51+
/// instead of grabbing the command from the PSCommand.
52+
/// </summary>
53+
public string InputString { get; set; }
54+
55+
/// <summary>
56+
/// If this is set, we will use this string for history and writing to the host
57+
/// instead of grabbing the command from the PSCommand.
58+
/// </summary>
59+
public bool UseNewScope { get; set; }
60+
4961
/// <summary>
5062
/// Gets or sets a value indicating whether the command to
5163
/// be executed is a console input prompt, such as the

src/PowerShellEditorServices/Services/PowerShellContext/Session/PSReadLinePromptContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal static bool TryGetPSReadLineProxy(
9191
{
9292
pwsh.Runspace = runspace;
9393
var psReadLineType = pwsh
94-
.AddScript(ReadLineInitScript)
94+
.AddScript(ReadLineInitScript, useLocalScope: true)
9595
.Invoke<Type>()
9696
.FirstOrDefault();
9797

src/PowerShellEditorServices/Services/PowerShellContext/Session/SessionDetails.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static PSCommand GetDetailsCommand()
5656
{
5757
PSCommand infoCommand = new PSCommand();
5858
infoCommand.AddScript(
59-
"@{ 'computerName' = if ([Environment]::MachineName) {[Environment]::MachineName} else {'localhost'}; 'processId' = $PID; 'instanceId' = $host.InstanceId }");
59+
"@{ 'computerName' = if ([Environment]::MachineName) {[Environment]::MachineName} else {'localhost'}; 'processId' = $PID; 'instanceId' = $host.InstanceId }",
60+
useLocalScope: true);
6061

6162
return infoCommand;
6263
}

0 commit comments

Comments
 (0)