From 1b0c5a4bcf15265010aaf7f0d7b94527546f6b4c Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 5 May 2020 19:12:51 -0700 Subject: [PATCH 1/2] clm weird api --- .../PowerShellContextService.cs | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index f79cbc4c2..3d5af082e 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Management.Automation.Host; @@ -16,15 +17,15 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.PowerShell.EditorServices.Handlers; +using Microsoft.PowerShell.EditorServices.Hosting; +using Microsoft.PowerShell.EditorServices.Logging; +using Microsoft.PowerShell.EditorServices.Services.PowerShellContext; using Microsoft.PowerShell.EditorServices.Utility; namespace Microsoft.PowerShell.EditorServices.Services { - using System.Diagnostics.CodeAnalysis; using System.Management.Automation; - using Microsoft.PowerShell.EditorServices.Handlers; - using Microsoft.PowerShell.EditorServices.Hosting; - using Microsoft.PowerShell.EditorServices.Services.PowerShellContext; /// /// Manages the lifetime and usage of a PowerShell session. @@ -1015,8 +1016,29 @@ public Task> ExecuteScriptStringAsync( { Validate.IsNotNull(nameof(scriptString), scriptString); + PSCommand command = null; + if(CurrentRunspace.Runspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage) + { + try + { + var scriptBlock = ScriptBlock.Create(scriptString); + PowerShell powerShell = scriptBlock.GetPowerShell(isTrustedInput: false, null); + command = powerShell.Commands; + } + catch (Exception e) + { + logger.LogException("Exception getting trusted/untrusted PSCommand.", e); + } + } + + // fall back to old behavior + if(command == null) + { + command = new PSCommand().AddScript(scriptString.Trim()); + } + return this.ExecuteCommandAsync( - new PSCommand().AddScript(scriptString.Trim()), + command, errorMessages, new ExecutionOptions() { From c0423f54091be38f65b6ab97a78c0bb259feeff6 Mon Sep 17 00:00:00 2001 From: "Tyler Leonhardt (POWERSHELL)" Date: Wed, 6 May 2020 10:16:50 -0700 Subject: [PATCH 2/2] codacy --- .../Services/PowerShellContext/PowerShellContextService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 3d5af082e..e4508d13f 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -1022,8 +1022,8 @@ public Task> ExecuteScriptStringAsync( try { var scriptBlock = ScriptBlock.Create(scriptString); - PowerShell powerShell = scriptBlock.GetPowerShell(isTrustedInput: false, null); - command = powerShell.Commands; + PowerShell ps = scriptBlock.GetPowerShell(isTrustedInput: false, null); + command = ps.Commands; } catch (Exception e) {