From c8c7ad1aa74cf51283cf21ec288a3d4abf7b567a Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 4 Nov 2019 12:28:18 -0800 Subject: [PATCH 1/3] Clean up the PowerShellConfig code a bit --- .../engine/PSConfiguration.cs | 48 ++++++------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index a52b7dfe75f..feecfa12b09 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"); + string valueName = GetExecutionPolicySettingKey(shellId); 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"); - } - - internal void SetConsolePrompting(bool shouldPrompt) + private string GetExecutionPolicySettingKey(string shellId) { - WriteValueToFile(ConfigScope.AllUsers, "ConsolePrompting", shouldPrompt); + return shellId == Utils.DefaultPowerShellShellID + ? ExecutionPolicyDefaultShellKey + : string.Concat(shellId, ":", "ExecutionPolicy"); } /// From e75fee9459ee13a4784829be6df01b5b4eedda58 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 4 Nov 2019 12:29:14 -0800 Subject: [PATCH 2/3] rename a variable --- src/System.Management.Automation/engine/PSConfiguration.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index feecfa12b09..500ec65e311 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -167,8 +167,8 @@ internal void RemoveExecutionPolicy(ConfigScope scope, string shellId) internal void SetExecutionPolicy(ConfigScope scope, string shellId, string executionPolicy) { - string valueName = GetExecutionPolicySettingKey(shellId); - WriteValueToFile(scope, valueName, executionPolicy); + string key = GetExecutionPolicySettingKey(shellId); + WriteValueToFile(scope, key, executionPolicy); } private string GetExecutionPolicySettingKey(string shellId) From a0e417fe948e492f7c3f49afe3dda965e751f455 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 5 Nov 2019 10:12:55 -0800 Subject: [PATCH 3/3] Address Ilya's comment --- src/System.Management.Automation/engine/PSConfiguration.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index 500ec65e311..01d12ae758c 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -173,7 +173,7 @@ internal void SetExecutionPolicy(ConfigScope scope, string shellId, string execu private string GetExecutionPolicySettingKey(string shellId) { - return shellId == Utils.DefaultPowerShellShellID + return string.Equals(shellId, Utils.DefaultPowerShellShellID, StringComparison.Ordinal) ? ExecutionPolicyDefaultShellKey : string.Concat(shellId, ":", "ExecutionPolicy"); }