From d5e20228232b8cedfc48a2442ba43c961736d4f6 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 27 Feb 2020 19:22:04 -0800 Subject: [PATCH 1/3] Fix startup in AllSigned execution policy --- .../PowerShellContext/PowerShellContextService.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 8bf56b7a3..a54a69e7e 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -367,11 +367,6 @@ public void Initialize( powerShellVersion.ToString()); } - if (VersionUtils.IsWindows) - { - this.SetExecutionPolicy(); - } - // Set up the runspace this.ConfigureRunspace(this.CurrentRunspace); @@ -428,6 +423,11 @@ public void Initialize( { this.PromptContext = new LegacyReadLineContext(this); } + + if (VersionUtils.IsWindows) + { + this.SetExecutionPolicy(); + } } /// From d3ca4418f19c4df122a0ea1db207a1e8121b79b8 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 27 Feb 2020 19:32:46 -0800 Subject: [PATCH 2/3] Fix ExecutionPolicy precedence --- .../PowerShellContextService.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index a54a69e7e..f3c31443d 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -2102,25 +2102,24 @@ private void SetExecutionPolicy() // - Process // - CurrentUser // - LocalMachine - // This is the order of precedence we want to follow, skipping the Process scope + // We want to ignore policy settings, since we'll already have those anyway. + // Then we need to look at the CurrentUser setting, and then the LocalMachine setting. // // Get-ExecutionPolicy -List emits PSObjects with Scope and ExecutionPolicy note properties // set to expected values, so we must sift through those. + ExecutionPolicy policyToSet = ExecutionPolicy.Bypass; - for (int i = policies.Count - 1; i >= 0; i--) + var currentUserPolicy = (ExecutionPolicy)policies[policies.Count - 2].Members["ExecutionPolicy"].Value; + if (currentUserPolicy != ExecutionPolicy.Undefined) { - PSObject policyObject = policies[i]; - - if ((ExecutionPolicyScope)policyObject.Members["Scope"].Value == ExecutionPolicyScope.Process) - { - break; - } - - var executionPolicy = (ExecutionPolicy)policyObject.Members["ExecutionPolicy"].Value; - if (executionPolicy != ExecutionPolicy.Undefined) + policyToSet = currentUserPolicy + } + else + { + var localMachinePolicy = (ExecutionPolicy)policies[policies.Count - 1].Members["ExecutionPolicy"].Value; + if (localMachinePolicy != ExecutionPolicy.Undefined) { - policyToSet = executionPolicy; - break; + policyToSet = localMachinePolicy; } } From ff23e1b798222a5f47818844edb3387a2e324da9 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 27 Feb 2020 19:34:29 -0800 Subject: [PATCH 3/3] Add semicolon --- .../Services/PowerShellContext/PowerShellContextService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index f3c31443d..165a01e13 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -2112,7 +2112,7 @@ private void SetExecutionPolicy() var currentUserPolicy = (ExecutionPolicy)policies[policies.Count - 2].Members["ExecutionPolicy"].Value; if (currentUserPolicy != ExecutionPolicy.Undefined) { - policyToSet = currentUserPolicy + policyToSet = currentUserPolicy; } else {