Skip to content

Fix startup in AllSigned execution policy #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,6 @@ public void Initialize(
powerShellVersion.ToString());
}

if (VersionUtils.IsWindows)
{
this.SetExecutionPolicy();
}

// Set up the runspace
this.ConfigureRunspace(this.CurrentRunspace);

Expand Down Expand Up @@ -428,6 +423,11 @@ public void Initialize(
{
this.PromptContext = new LegacyReadLineContext(this);
}

if (VersionUtils.IsWindows)
{
this.SetExecutionPolicy();
}
}

/// <summary>
Expand Down Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, can we rely on the order to remain consistent though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other alternative is to look for user scope and then to look for machine scope

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I don't think there's any plan to update anything around execution policy

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;
}
}

Expand Down