Skip to content

Commit 453166b

Browse files
Use API on ScriptBlock to generate PSCommand to run in ConstrainedLanguage mode (#1285)
* clm weird api * codacy Co-authored-by: Tyler Leonhardt (POWERSHELL) <[email protected]>
1 parent 6b50650 commit 453166b

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.ComponentModel;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
1011
using System.Linq;
1112
using System.Management.Automation.Host;
@@ -16,15 +17,15 @@
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using Microsoft.Extensions.Logging;
20+
using Microsoft.PowerShell.EditorServices.Handlers;
21+
using Microsoft.PowerShell.EditorServices.Hosting;
22+
using Microsoft.PowerShell.EditorServices.Logging;
23+
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
1924
using Microsoft.PowerShell.EditorServices.Utility;
2025

2126
namespace Microsoft.PowerShell.EditorServices.Services
2227
{
23-
using System.Diagnostics.CodeAnalysis;
2428
using System.Management.Automation;
25-
using Microsoft.PowerShell.EditorServices.Handlers;
26-
using Microsoft.PowerShell.EditorServices.Hosting;
27-
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
2829

2930
/// <summary>
3031
/// Manages the lifetime and usage of a PowerShell session.
@@ -1015,8 +1016,29 @@ public Task<IEnumerable<object>> ExecuteScriptStringAsync(
10151016
{
10161017
Validate.IsNotNull(nameof(scriptString), scriptString);
10171018

1019+
PSCommand command = null;
1020+
if(CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
1021+
{
1022+
try
1023+
{
1024+
var scriptBlock = ScriptBlock.Create(scriptString);
1025+
PowerShell ps = scriptBlock.GetPowerShell(isTrustedInput: false, null);
1026+
command = ps.Commands;
1027+
}
1028+
catch (Exception e)
1029+
{
1030+
logger.LogException("Exception getting trusted/untrusted PSCommand.", e);
1031+
}
1032+
}
1033+
1034+
// fall back to old behavior
1035+
if(command == null)
1036+
{
1037+
command = new PSCommand().AddScript(scriptString.Trim());
1038+
}
1039+
10181040
return this.ExecuteCommandAsync<object>(
1019-
new PSCommand().AddScript(scriptString.Trim()),
1041+
command,
10201042
errorMessages,
10211043
new ExecutionOptions()
10221044
{

0 commit comments

Comments
 (0)