@@ -47,7 +47,8 @@ public enum ConfigScope
47
47
/// </remarks>
48
48
internal sealed class PowerShellConfig
49
49
{
50
- private const string configFileName = "powershell.config.json" ;
50
+ private const string ConfigFileName = "powershell.config.json" ;
51
+ private const string ExecutionPolicyDefaultShellKey = "Microsoft.PowerShell:ExecutionPolicy" ;
51
52
52
53
// Provide a singleton
53
54
internal static readonly PowerShellConfig Instance = new PowerShellConfig ( ) ;
@@ -79,13 +80,13 @@ private PowerShellConfig()
79
80
{
80
81
// Sets the system-wide configuration file.
81
82
systemWideConfigDirectory = Utils . DefaultPowerShellAppBase ;
82
- systemWideConfigFile = Path . Combine ( systemWideConfigDirectory , configFileName ) ;
83
+ systemWideConfigFile = Path . Combine ( systemWideConfigDirectory , ConfigFileName ) ;
83
84
84
85
// Sets the per-user configuration directory
85
86
// Note: This directory may or may not exist depending upon the execution scenario.
86
87
// Writes will attempt to create the directory if it does not already exist.
87
88
perUserConfigDirectory = Platform . ConfigDirectory ;
88
- perUserConfigFile = Path . Combine ( perUserConfigDirectory , configFileName ) ;
89
+ perUserConfigFile = Path . Combine ( perUserConfigDirectory , ConfigFileName ) ;
89
90
90
91
emptyConfig = new JObject ( ) ;
91
92
configRoots = new JObject [ 2 ] ;
@@ -153,49 +154,28 @@ internal string GetModulePath(ConfigScope scope)
153
154
/// <returns>The execution policy if found. Null otherwise.</returns>
154
155
internal string GetExecutionPolicy ( ConfigScope scope , string shellId )
155
156
{
156
- string execPolicy = null ;
157
-
158
- string valueName = string . Concat ( shellId , ":" , "ExecutionPolicy" ) ;
159
- string rawExecPolicy = ReadValueFromFile < string > ( scope , valueName ) ;
160
-
161
- if ( ! string . IsNullOrEmpty ( rawExecPolicy ) )
162
- {
163
- execPolicy = rawExecPolicy ;
164
- }
165
-
166
- return execPolicy ;
157
+ string key = GetExecutionPolicySettingKey ( shellId ) ;
158
+ string execPolicy = ReadValueFromFile < string > ( scope , key ) ;
159
+ return string . IsNullOrEmpty ( execPolicy ) ? null : execPolicy ;
167
160
}
168
161
169
162
internal void RemoveExecutionPolicy ( ConfigScope scope , string shellId )
170
163
{
171
- string valueName = string . Concat ( shellId , ":" , "ExecutionPolicy" ) ;
172
- RemoveValueFromFile < string > ( scope , valueName ) ;
164
+ string key = GetExecutionPolicySettingKey ( shellId ) ;
165
+ RemoveValueFromFile < string > ( scope , key ) ;
173
166
}
174
167
175
168
internal void SetExecutionPolicy ( ConfigScope scope , string shellId , string executionPolicy )
176
169
{
177
- string valueName = string . Concat ( shellId , ":" , "ExecutionPolicy" ) ;
178
- WriteValueToFile < string > ( scope , valueName , executionPolicy ) ;
179
- }
180
-
181
- /// <summary>
182
- /// Existing Key = HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds
183
- /// Proposed value = existing default. Probably "1"
184
- ///
185
- /// Schema:
186
- /// {
187
- /// "ConsolePrompting" : bool
188
- /// }
189
- /// </summary>
190
- /// <returns>Whether console prompting should happen. If the value cannot be read it defaults to false.</returns>
191
- internal bool GetConsolePrompting ( )
192
- {
193
- return ReadValueFromFile < bool > ( ConfigScope . AllUsers , "ConsolePrompting" ) ;
170
+ string key = GetExecutionPolicySettingKey ( shellId ) ;
171
+ WriteValueToFile < string > ( scope , key , executionPolicy ) ;
194
172
}
195
173
196
- internal void SetConsolePrompting ( bool shouldPrompt )
174
+ private string GetExecutionPolicySettingKey ( string shellId )
197
175
{
198
- WriteValueToFile < bool > ( ConfigScope . AllUsers , "ConsolePrompting" , shouldPrompt ) ;
176
+ return string . Equals ( shellId , Utils . DefaultPowerShellShellID , StringComparison . Ordinal )
177
+ ? ExecutionPolicyDefaultShellKey
178
+ : string . Concat ( shellId , ":" , "ExecutionPolicy" ) ;
199
179
}
200
180
201
181
/// <summary>
0 commit comments