PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
496 stars 94 forks source link

I want the ZIP install of PowerShell Core to use the current working directory as the installation directory for modules #631

Closed sdg002 closed 2 years ago

sdg002 commented 2 years ago

Summary of the new feature / enhancement

As an user in a very restricted environment I cannot use PowerShell Core V7 on Windows 10 because of the following reasons acting simultaneously:

image

All the above make it impossible to use any module thereby rendering PowerShell Core almost useless in my current work environment.

What workarounds did I try?

  1. I installed the ZIP download of PowerShell Core in a local folder C:\Users\johndoe\mywork001, outside of Documents hierarchy to bypass Anti-Virus.
  2. I tried tinkering with $env:PSModulePath in the vain hope that PWSH would start using the aforementioned folder for installing modules.
  3. I tried altering the powershell.config.json as per this link , but this too did not work

Possible solution - inspired by NPM

image

Taking inspiration from Node Package Manager works (NPM) , PowerShell Core should not rely on a hard wired set of folder(s) for installing modules. Instead, it should be allowed to use the current working directory. Think of a package.json in the case of NPM and the JavaScript modules get installed in the node_modules sub-folder of the current working directory

Possible solution - inspired by powershell.config.json

I should be able to create a powershell.config.json in any folder c:\users\johndoe\mywork001\ and able to run pwsh.exe located in another folder by specifying the absolute path to the powershel.config.json as a command line argument. The PWSH.exe instance should follow the directions of the configuration file and install modules accordingly

Overarching idea

Make PWSH free from the shackles of CurrentUser and AllUsers scope. The CurrentUser/AllUser could still be retained as good to have options. I should be able to run side by side copies of PWSH using different versions of Modules , just like XCOPY of .NET or NODE.JS.

Proposed technical implementation details (optional)

Installing a module locally could look something like the following:

install-module -Name SqlServer -Scope Local

If this were NPM, then you would do the following:

npm install sqlserver

You will see the entry of SqlServer getting added to package.json and the Jascript code downloaded to node_modules folder.

The benefit of package.json is the ability to have SCM on your environment. You can reproduce your working folder by simply doing a npm install and all the packages listed in package.json will get downloaded with the right versions.

No response

daxian-dbw commented 2 years ago

@alerickson @anamnavi Does PowerShellGet v3 allows the user to specify where to install a module? If not, maybe you want to consider doing so.

alerickson commented 2 years ago

@daxian-dbw v3 does allow users to specify where to install a module, in two different ways:
1) The user can modify the $env:PSModulePath and v3 will prioritize these paths over the default PowerShell paths 2) The user can run Save-PSResource <ModuleName> -IncludeXML -Path <InstallLocation>

@sdg002 are you still blocked with this issue? Do you need assistance with it or are just filing a report?

daxian-dbw commented 2 years ago

PSv2 can do Save-Module which is similar to Save-PSResource.

The user can modify the $env:PSModulePath and v3 will prioritize these paths over the default PowerShell paths

When $env:PSModulePath is changed to not include C:\Users\johndoe\Documents\PowerShell\Modules, where will the module be installed when using -Scope CurrentUser in v3?

alerickson commented 2 years ago

It'll look at whatever paths are in $env:PSModulePath and find the next available path that ends in \Modules. If it doesn't find any appropriate paths there, it'll install to C:\Users\johndoe\Documents\PowerShell\Modules

daxian-dbw commented 2 years ago

It sounds like that's what @sdg002 was asking :)

sdg002 commented 2 years ago

Thank you @alerickson and @daxian-dbw for taking time to look into this.

Yes - I managed to find a work around using a combination of Find-Module , Save-Module and alterations to $env:PSModulePath .
I will document the steps here , if in case somebody faces the same predicament as I did.

Background

What was the root cause?

What was the workaround?

Follow these steps when specifically installing any PWSH modules

Follow this step for using PWSH for any other scenario

Suggestion

Improve the error message when -verbose flag is used

PowerShell should clearly state attempting to load DLL xyz.dll from path c:\blah1\blah2 and failed ... . The mentioning of the absolute path makes it very explicit for me.

Python and NodeJS have no problem in this scenario- why should PowerShell core suffer?

This is my motivation for the suggestion that PWSH should be

Conclusion

PWSH is such a great scripting tool. This is one of the best things to come out of Micrsoft. I propose we make it even better. With the backing of .NET, sky is the limit for PWSH.

Thanks