Skip to content

Commit a454cb3

Browse files
committed
Use runspace instead of reflection
1 parent d53dd08 commit a454cb3

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

src/code/Utils.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,8 @@ private static string GetHomeOrCreateTempHome()
11601160
private readonly static Version PSVersion7_7 = new Version(7, 7, 0);
11611161

11621162
/// <summary>
1163-
/// Gets the user content directory path using PowerShell's GetPSModulePath API.
1164-
/// Falls back to legacy path if the API is not available or PowerShell version is below 7.7.0.
1163+
/// Gets the user content directory path using PowerShell's Get-PSContentPath cmdlet.
1164+
/// Falls back to legacy path if the cmdlet is not available or PowerShell version is below 7.7.0.
11651165
/// </summary>
11661166
private static string GetUserContentPath(PSCmdlet psCmdlet, string legacyPath)
11671167
{
@@ -1171,38 +1171,41 @@ private static string GetUserContentPath(PSCmdlet psCmdlet, string legacyPath)
11711171
? version
11721172
: new Version(5, 1);
11731173

1174-
// Only use GetPSModulePath API if PowerShell version is 7.7.0 or greater (when PSContentPath feature is available)
1174+
// Only use Get-PSContentPath cmdlet if PowerShell version is 7.7.0 or greater (when PSContentPath feature is available)
11751175
if (psVersion >= PSVersion7_7)
11761176
{
1177-
// Try to use PowerShell's GetPSModulePath API via reflection
1178-
// This API automatically respects PSContentPath settings
1177+
// Try to use PowerShell's Get-PSContentPath cmdlet
1178+
// This cmdlet automatically respects PSContentPath settings
11791179
try
11801180
{
1181-
var moduleIntrinsicsType = typeof(PSModuleInfo).Assembly.GetType("System.Management.Automation.ModuleIntrinsics");
1182-
var scopeEnumType = typeof(PSModuleInfo).Assembly.GetType("System.Management.Automation.PSModulePathScope");
1183-
1184-
if (moduleIntrinsicsType != null && scopeEnumType != null)
1181+
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
11851182
{
1186-
var getPSModulePathMethod = moduleIntrinsicsType.GetMethod("GetPSModulePath", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
1183+
pwsh.AddCommand("Get-PSContentPath");
1184+
var results = pwsh.Invoke();
11871185

1188-
if (getPSModulePathMethod != null)
1186+
if (!pwsh.HadErrors && results != null && results.Count > 0)
11891187
{
1190-
// PSModulePathScope.User = 0
1191-
object userScope = Enum.ToObject(scopeEnumType, 0);
1192-
string userModulePath = (string)getPSModulePathMethod.Invoke(null, new object[] { userScope });
1193-
1194-
if (!string.IsNullOrEmpty(userModulePath))
1188+
// Get-PSContentPath returns a PSObject, extract the path string
1189+
string userContentPath = results[0]?.ToString();
1190+
if (!string.IsNullOrEmpty(userContentPath))
11951191
{
1196-
string userContentPath = Path.GetDirectoryName(userModulePath);
1197-
psCmdlet.WriteVerbose($"User content path from GetPSModulePath API: {userContentPath}");
1192+
psCmdlet.WriteVerbose($"User content path from Get-PSContentPath: {userContentPath}");
11981193
return userContentPath;
11991194
}
12001195
}
1196+
1197+
if (pwsh.HadErrors)
1198+
{
1199+
foreach (var error in pwsh.Streams.Error)
1200+
{
1201+
psCmdlet.WriteVerbose($"Get-PSContentPath error: {error}");
1202+
}
1203+
}
12011204
}
12021205
}
12031206
catch (Exception ex)
12041207
{
1205-
psCmdlet.WriteVerbose($"GetPSModulePath API not available: {ex.Message}");
1208+
psCmdlet.WriteVerbose($"Get-PSContentPath cmdlet not available: {ex.Message}");
12061209
}
12071210
}
12081211
else

0 commit comments

Comments
 (0)