Skip to content

Commit 4f6cd32

Browse files
authored
Fix startup in AllSigned execution policy (#1214)
1 parent eba54cc commit 4f6cd32

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,6 @@ public void Initialize(
367367
powerShellVersion.ToString());
368368
}
369369

370-
if (VersionUtils.IsWindows)
371-
{
372-
this.SetExecutionPolicy();
373-
}
374-
375370
// Set up the runspace
376371
this.ConfigureRunspace(this.CurrentRunspace);
377372

@@ -428,6 +423,11 @@ public void Initialize(
428423
{
429424
this.PromptContext = new LegacyReadLineContext(this);
430425
}
426+
427+
if (VersionUtils.IsWindows)
428+
{
429+
this.SetExecutionPolicy();
430+
}
431431
}
432432

433433
/// <summary>
@@ -2102,25 +2102,24 @@ private void SetExecutionPolicy()
21022102
// - Process
21032103
// - CurrentUser
21042104
// - LocalMachine
2105-
// This is the order of precedence we want to follow, skipping the Process scope
2105+
// We want to ignore policy settings, since we'll already have those anyway.
2106+
// Then we need to look at the CurrentUser setting, and then the LocalMachine setting.
21062107
//
21072108
// Get-ExecutionPolicy -List emits PSObjects with Scope and ExecutionPolicy note properties
21082109
// set to expected values, so we must sift through those.
2110+
21092111
ExecutionPolicy policyToSet = ExecutionPolicy.Bypass;
2110-
for (int i = policies.Count - 1; i >= 0; i--)
2112+
var currentUserPolicy = (ExecutionPolicy)policies[policies.Count - 2].Members["ExecutionPolicy"].Value;
2113+
if (currentUserPolicy != ExecutionPolicy.Undefined)
21112114
{
2112-
PSObject policyObject = policies[i];
2113-
2114-
if ((ExecutionPolicyScope)policyObject.Members["Scope"].Value == ExecutionPolicyScope.Process)
2115-
{
2116-
break;
2117-
}
2118-
2119-
var executionPolicy = (ExecutionPolicy)policyObject.Members["ExecutionPolicy"].Value;
2120-
if (executionPolicy != ExecutionPolicy.Undefined)
2115+
policyToSet = currentUserPolicy;
2116+
}
2117+
else
2118+
{
2119+
var localMachinePolicy = (ExecutionPolicy)policies[policies.Count - 1].Members["ExecutionPolicy"].Value;
2120+
if (localMachinePolicy != ExecutionPolicy.Undefined)
21212121
{
2122-
policyToSet = executionPolicy;
2123-
break;
2122+
policyToSet = localMachinePolicy;
21242123
}
21252124
}
21262125

0 commit comments

Comments
 (0)