diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index 5724776d5..c3e571315 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -9,7 +9,6 @@ using Microsoft.PowerShell.EditorServices.Hosting; using Microsoft.PowerShell.EditorServices.Utility; using System.Collections.Generic; -using System.IO; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility { @@ -166,10 +165,13 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat { var profileVariable = new PSObject(); - pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + var psCommand = new PSCommand() + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + + pwsh.InvokeCommand(psCommand); pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable); } @@ -200,22 +202,6 @@ public static string GetErrorString(this PowerShell pwsh) return sb.ToString(); } - private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath) - { - profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); - - if (File.Exists(profilePath)) - { - var psCommand = new PSCommand() - .AddScript(profilePath, useLocalScope: false) - .AddOutputCommand(); - - pwsh.InvokeCommand(psCommand); - } - - return pwsh; - } - private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex) { sb.Append("Error #").Append(errorIndex).Append(':').AppendLine() diff --git a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs index 55c8eed20..634ec2674 100644 --- a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs +++ b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.IO; using System.Linq.Expressions; using System.Management.Automation; using System.Management.Automation.Runspaces; @@ -61,6 +62,21 @@ public static PSCommand MergePipelineResults(this PSCommand psCommand) return psCommand; } + public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObject profileVariable, string profileName, string profilePath) + { + profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); + + if (File.Exists(profilePath)) + { + psCommand + .AddCommand(profilePath, useLocalScope: false) + .AddOutputCommand() + .AddStatement(); + } + + return psCommand; + } + /// /// Get a representation of the PSCommand, for logging purposes. ///