From 118eea98804e65f9a2940197b3fda5b5c603b68b Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:52:52 -0800 Subject: [PATCH] Always allow PSScriptAnalzyer to load --- .../Services/Analysis/PssaCmdletAnalysisEngine.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs b/src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs index 1ed1c04de..c460a3020 100644 --- a/src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs +++ b/src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.PowerShell.EditorServices.Services.TextDocument; +using Microsoft.PowerShell.EditorServices.Utility; namespace Microsoft.PowerShell.EditorServices.Services.Analysis { @@ -360,6 +361,14 @@ private static RunspacePool CreatePssaRunspacePool(string pssaModulePath) // We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` // only, which is a more minimal and therefore safer state. InitialSessionState sessionState = InitialSessionState.CreateDefault2(); + + // We set the runspace's execution policy `Bypass` so we can always import our bundled + // PSScriptAnalyzer module. + if (VersionUtils.IsWindows) + { + sessionState.ExecutionPolicy = ExecutionPolicy.Bypass; + } + sessionState.ImportPSModulesFromPath(pssaModulePath); RunspacePool runspacePool = RunspaceFactory.CreateRunspacePool(sessionState);