diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index a52b7dfe75f..01d12ae758c 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -47,7 +47,8 @@ public enum ConfigScope /// internal sealed class PowerShellConfig { - private const string configFileName = "powershell.config.json"; + private const string ConfigFileName = "powershell.config.json"; + private const string ExecutionPolicyDefaultShellKey = "Microsoft.PowerShell:ExecutionPolicy"; // Provide a singleton internal static readonly PowerShellConfig Instance = new PowerShellConfig(); @@ -79,13 +80,13 @@ private PowerShellConfig() { // Sets the system-wide configuration file. systemWideConfigDirectory = Utils.DefaultPowerShellAppBase; - systemWideConfigFile = Path.Combine(systemWideConfigDirectory, configFileName); + systemWideConfigFile = Path.Combine(systemWideConfigDirectory, ConfigFileName); // Sets the per-user configuration directory // Note: This directory may or may not exist depending upon the execution scenario. // Writes will attempt to create the directory if it does not already exist. perUserConfigDirectory = Platform.ConfigDirectory; - perUserConfigFile = Path.Combine(perUserConfigDirectory, configFileName); + perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName); emptyConfig = new JObject(); configRoots = new JObject[2]; @@ -153,49 +154,28 @@ internal string GetModulePath(ConfigScope scope) /// The execution policy if found. Null otherwise. internal string GetExecutionPolicy(ConfigScope scope, string shellId) { - string execPolicy = null; - - string valueName = string.Concat(shellId, ":", "ExecutionPolicy"); - string rawExecPolicy = ReadValueFromFile(scope, valueName); - - if (!string.IsNullOrEmpty(rawExecPolicy)) - { - execPolicy = rawExecPolicy; - } - - return execPolicy; + string key = GetExecutionPolicySettingKey(shellId); + string execPolicy = ReadValueFromFile(scope, key); + return string.IsNullOrEmpty(execPolicy) ? null : execPolicy; } internal void RemoveExecutionPolicy(ConfigScope scope, string shellId) { - string valueName = string.Concat(shellId, ":", "ExecutionPolicy"); - RemoveValueFromFile(scope, valueName); + string key = GetExecutionPolicySettingKey(shellId); + RemoveValueFromFile(scope, key); } internal void SetExecutionPolicy(ConfigScope scope, string shellId, string executionPolicy) { - string valueName = string.Concat(shellId, ":", "ExecutionPolicy"); - WriteValueToFile(scope, valueName, executionPolicy); - } - - /// - /// Existing Key = HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds - /// Proposed value = existing default. Probably "1" - /// - /// Schema: - /// { - /// "ConsolePrompting" : bool - /// } - /// - /// Whether console prompting should happen. If the value cannot be read it defaults to false. - internal bool GetConsolePrompting() - { - return ReadValueFromFile(ConfigScope.AllUsers, "ConsolePrompting"); + string key = GetExecutionPolicySettingKey(shellId); + WriteValueToFile(scope, key, executionPolicy); } - internal void SetConsolePrompting(bool shouldPrompt) + private string GetExecutionPolicySettingKey(string shellId) { - WriteValueToFile(ConfigScope.AllUsers, "ConsolePrompting", shouldPrompt); + return string.Equals(shellId, Utils.DefaultPowerShellShellID, StringComparison.Ordinal) + ? ExecutionPolicyDefaultShellKey + : string.Concat(shellId, ":", "ExecutionPolicy"); } ///