|
9 | 9 | using Microsoft.PowerShell.EditorServices.Hosting;
|
10 | 10 | using Microsoft.PowerShell.EditorServices.Utility;
|
11 | 11 | using System.Collections.Generic;
|
12 |
| -using System.IO; |
13 | 12 |
|
14 | 13 | namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
|
15 | 14 | {
|
@@ -164,14 +163,24 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge
|
164 | 163 |
|
165 | 164 | public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePaths)
|
166 | 165 | {
|
167 |
| - var profileVariable = new PSObject(); |
| 166 | + // Per the documentation, "the `$PROFILE` variable stores the path to the 'Current User, |
| 167 | + // Current Host' profile. The other profiles are saved in note properties of the |
| 168 | + // `$PROFILE` variable. Its type is `String`. |
| 169 | + // |
| 170 | + // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1#the-profile-variable |
| 171 | + var profileVariable = PSObject.AsPSObject(profilePaths.CurrentUserCurrentHost); |
168 | 172 |
|
169 |
| - pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) |
170 |
| - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) |
171 |
| - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) |
172 |
| - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); |
| 173 | + var psCommand = new PSCommand() |
| 174 | + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) |
| 175 | + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) |
| 176 | + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) |
| 177 | + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); |
173 | 178 |
|
| 179 | + // NOTE: This must be set before the profiles are loaded. |
174 | 180 | pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable);
|
| 181 | + |
| 182 | + pwsh.InvokeCommand(psCommand); |
| 183 | + |
175 | 184 | }
|
176 | 185 |
|
177 | 186 | public static void ImportModule(this PowerShell pwsh, string moduleNameOrPath)
|
@@ -200,22 +209,6 @@ public static string GetErrorString(this PowerShell pwsh)
|
200 | 209 | return sb.ToString();
|
201 | 210 | }
|
202 | 211 |
|
203 |
| - private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath) |
204 |
| - { |
205 |
| - profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); |
206 |
| - |
207 |
| - if (File.Exists(profilePath)) |
208 |
| - { |
209 |
| - var psCommand = new PSCommand() |
210 |
| - .AddScript(profilePath, useLocalScope: false) |
211 |
| - .AddOutputCommand(); |
212 |
| - |
213 |
| - pwsh.InvokeCommand(psCommand); |
214 |
| - } |
215 |
| - |
216 |
| - return pwsh; |
217 |
| - } |
218 |
| - |
219 | 212 | private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex)
|
220 | 213 | {
|
221 | 214 | sb.Append("Error #").Append(errorIndex).Append(':').AppendLine()
|
|
0 commit comments