11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
3-
3+ #nullable enable
44using System ;
55using System . Collections . Generic ;
66using System . Collections . ObjectModel ;
@@ -69,7 +69,7 @@ and not PSInvocationState.Failed
6969 pwsh . InvocationStateChanged += handler ;
7070 }
7171
72- public static Collection < TResult > InvokeAndClear < TResult > ( this PowerShell pwsh , PSInvocationSettings invocationSettings = null )
72+ public static Collection < TResult > InvokeAndClear < TResult > ( this PowerShell pwsh , PSInvocationSettings ? invocationSettings = null )
7373 {
7474 try
7575 {
@@ -81,7 +81,7 @@ public static Collection<TResult> InvokeAndClear<TResult>(this PowerShell pwsh,
8181 }
8282 }
8383
84- public static void InvokeAndClear ( this PowerShell pwsh , PSInvocationSettings invocationSettings = null )
84+ public static void InvokeAndClear ( this PowerShell pwsh , PSInvocationSettings ? invocationSettings = null )
8585 {
8686 try
8787 {
@@ -93,13 +93,13 @@ public static void InvokeAndClear(this PowerShell pwsh, PSInvocationSettings inv
9393 }
9494 }
9595
96- public static Collection < TResult > InvokeCommand < TResult > ( this PowerShell pwsh , PSCommand psCommand , PSInvocationSettings invocationSettings = null )
96+ public static Collection < TResult > InvokeCommand < TResult > ( this PowerShell pwsh , PSCommand psCommand , PSInvocationSettings ? invocationSettings = null )
9797 {
9898 pwsh . Commands = psCommand ;
9999 return pwsh . InvokeAndClear < TResult > ( invocationSettings ) ;
100100 }
101101
102- public static void InvokeCommand ( this PowerShell pwsh , PSCommand psCommand , PSInvocationSettings invocationSettings = null )
102+ public static void InvokeCommand ( this PowerShell pwsh , PSCommand psCommand , PSInvocationSettings ? invocationSettings = null )
103103 {
104104 pwsh . Commands = psCommand ;
105105 pwsh . InvokeAndClear ( invocationSettings ) ;
@@ -193,7 +193,7 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge
193193 }
194194 }
195195
196- public static void LoadProfiles ( this PowerShell pwsh , ProfilePathInfo profilePaths )
196+ public static void SetProfileVariable ( this PowerShell pwsh , ProfilePathInfo profilePaths )
197197 {
198198 // Per the documentation, "the `$PROFILE` variable stores the path to the 'Current User,
199199 // Current Host' profile. The other profiles are saved in note properties of the
@@ -202,15 +202,24 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat
202202 // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1#the-profile-variable
203203 PSObject profileVariable = PSObject . AsPSObject ( profilePaths . CurrentUserCurrentHost ) ;
204204
205+ profileVariable . Members . Add ( new PSNoteProperty ( nameof ( profilePaths . AllUsersAllHosts ) , profilePaths . AllUsersAllHosts ) ) ;
206+ profileVariable . Members . Add ( new PSNoteProperty ( nameof ( profilePaths . AllUsersCurrentHost ) , profilePaths . AllUsersCurrentHost ) ) ;
207+ profileVariable . Members . Add ( new PSNoteProperty ( nameof ( profilePaths . CurrentUserAllHosts ) , profilePaths . CurrentUserAllHosts ) ) ;
208+ profileVariable . Members . Add ( new PSNoteProperty ( nameof ( profilePaths . CurrentUserCurrentHost ) , profilePaths . CurrentUserCurrentHost ) ) ;
209+
210+ pwsh . Runspace . SessionStateProxy . SetVariable ( "PROFILE" , profileVariable ) ;
211+ }
212+
213+ public static void LoadProfileScripts ( this PowerShell pwsh , ProfilePathInfo profilePaths )
214+ {
215+ PSObject profileVariable = PSObject . AsPSObject ( profilePaths . CurrentUserCurrentHost ) ;
216+
205217 PSCommand psCommand = new PSCommand ( )
206218 . AddProfileLoadIfExists ( profileVariable , nameof ( profilePaths . AllUsersAllHosts ) , profilePaths . AllUsersAllHosts )
207219 . AddProfileLoadIfExists ( profileVariable , nameof ( profilePaths . AllUsersCurrentHost ) , profilePaths . AllUsersCurrentHost )
208220 . AddProfileLoadIfExists ( profileVariable , nameof ( profilePaths . CurrentUserAllHosts ) , profilePaths . CurrentUserAllHosts )
209221 . AddProfileLoadIfExists ( profileVariable , nameof ( profilePaths . CurrentUserCurrentHost ) , profilePaths . CurrentUserCurrentHost ) ;
210222
211- // NOTE: This must be set before the profiles are loaded.
212- pwsh . Runspace . SessionStateProxy . SetVariable ( "PROFILE" , profileVariable ) ;
213-
214223 // NOTE: Because it's possible there are no profiles defined, we might have an empty
215224 // command. Since this is being executed directly, we can't rely on `ThrowOnError =
216225 // false` to avoid an exception here. Instead, we must just not execute it.
@@ -253,7 +262,7 @@ private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord e
253262 . AppendLine ( "Exception:" )
254263 . Append ( " " ) . Append ( error . Exception . ToString ( ) ?? "<null>" ) ;
255264
256- Exception innerException = error . Exception ? . InnerException ;
265+ Exception ? innerException = error . Exception ? . InnerException ;
257266 while ( innerException != null )
258267 {
259268 sb . AppendLine ( "InnerException:" )
0 commit comments