Repository navigation
Get-PSResource does not search in user context environment variable PSModulePath #889
Copy link
Copy link
Open
Milestone
Description
Activity
o-l-a-v commented
on Apr 1, 2024 ContributorAuthorMore actionsHere's the responsible code as far as I can see:
PSResourceGet/src/code/Utils.cs
Lines 993 to 1017 in b28fba0
// Find all potential resource paths public static List<string> GetPathsFromEnvVarAndScope( PSCmdlet psCmdlet, ScopeType? scope) { GetStandardPlatformPaths( psCmdlet, out string myDocumentsPath, out string programFilesPath); List<string> resourcePaths = new List<string>(); if (scope is null || scope.Value is ScopeType.CurrentUser) { resourcePaths.Add(Path.Combine(myDocumentsPath, "Modules")); resourcePaths.Add(Path.Combine(myDocumentsPath, "Scripts")); } if (scope.Value is ScopeType.AllUsers) { resourcePaths.Add(Path.Combine(programFilesPath, "Modules")); resourcePaths.Add(Path.Combine(programFilesPath, "Scripts")); } return resourcePaths; } Seems doable to add PSModulePath here?
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && scope is not null) { Environment.GetEnvironmentVariable( "PSModulePath", scope.Value is ScopeType.CurrentUser ? EnvironmentVariableTarget.User : EnvironmentVariableTarget.Machine ); }
I understand it's more complex than that. One must have tests etc.
Is this one up for grabs, or do you not want environment variable
PSModulePathto have any say?
Edit: Here's a PowerShellers attempt. 😁
Click to view
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && scope is not null) { string PSModulePath = Environment.GetEnvironmentVariable( "PSModulePath", scope.Value is ScopeType.CurrentUser ? EnvironmentVariableTarget.User : EnvironmentVariableTarget.Machine ); if (string.IsNullOrEmpty(PSModulePath)) { psCmdlet.WriteVerbose("Found no value for PSModulePath given the scope."); } else { foreach (string element in PSModulePath.Split(Path.PathSeparator)) { if (Directory.Exists(element)) { if ( string.Equals( element.Split(Path.DirectorySeparatorChar).Last(), "Modules", StringComparison.OrdinalIgnoreCase ) ) { psCmdlet.WriteVerbose( string.Format( "'{0}' exists, adding it to 'resourcePaths'.", element ) ); resourcePaths.Add(element); resourcePaths.Add(Path.Combine(Directory.GetParent(element).FullName, "Scripts")); } else { psCmdlet.WriteVerbose( string.Format( "'{0}' exists but does not match expected pattern, not adding it to 'resourcePaths'.", element ) ); } } else { psCmdlet.WriteVerbose( string.Format( "'{0}' does not exist, not adding it to 'resourcePaths'.", element ) ); } } } }
Prerequisites
Steps to reproduce
PackageManagement,PowerShellGetv2andPowerShellGet3.0.18-beta (and prior versions) does not search in directorories specified in user context environmental variablePSModulePathwhen searching for modules. But at least v3 has a-Pathparameter.Reproduce:
Az.Accountsto%LOCALAPPDATA%\Microsoft\PowerShell\Modulesby usingSave-PSResource.$env:PSModulePath.%LOCALAPPDATA%\Microsoft\PowerShell\Modulesis specified inHKCU:\Environment\PSModulePath, and that PowerShell loaded it into$env:PSModulePathat start. Should also be seen in[System.Environment]::GetEnvironmentVariable('PSModulePath','User').PowerShellGet\Get-PSResource -Name 'Az.Accounts'. Result: Not found.-Scope 'CurrentUser: Nope, still not found.-Path ('{0}\Microsoft\PowerShell\Modules' -f $env:LOCALAPPDATA): Found.Microsoft.PowerShell.Core\Import-ModuleandGet-Moduleworks as expected.Expected behavior
PowerShellGet\Get-PSResource should search in all PSModulePath paths, also those specified in user context environmental variable.Actual behavior
It does not.Error details
No response
Environment data
Visuals
No response