nvs icon indicating copy to clipboard operation
nvs copied to clipboard

nvs and node aren't installed as global commands when PowerShell execution policy is restricted

Open StarpTech opened this issue 7 years ago • 15 comments

Steps to reproduce

  1. Install via choco
  2. Run new powershell and type nvs but command could not be found
  3. Run nvs script from app directory directly
  4. Install latest node.js version
  5. Run new powershell and node could not be found

Environment

OS: Windows 10 NSV: 1.5.1

StarpTech avatar Oct 19 '18 19:10 StarpTech

The nvs installer is supposed to modify your user profile PATH environment variable so that the nvs command is available in any new shell. And that works in my testing... Maybe that step of the installation failed for you?

Try running nvs install. That should re-apply the PATH change both in the current window and in your user profile. Does it show any output or errors?

jasongin avatar Oct 19 '18 19:10 jasongin

Run new powershell

At this step, did you start the new powershell in a new window? Or from the same window where you ran the choco command?

jasongin avatar Oct 19 '18 19:10 jasongin

At this step, did you start the new powershell in a new window? Or from the same window where you ran the choco command?

I started a new window.

StarpTech avatar Oct 19 '18 19:10 StarpTech

Can you try the nvs install command I mentioned above?

jasongin avatar Oct 19 '18 21:10 jasongin

Hi @jasongin I could identify the issue. nvs is correctly installed but my system hasn't access to it due to Microsoft default execution policy.

This solves the issue:

  • Run Administrator shell and run:
Set-ExecutionPolicy RemoteSigned
  • I allowed all in my case
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
  • Run nvs
  • Works!

StarpTech avatar Oct 20 '18 15:10 StarpTech

Thanks @StarpTech! I have been using nvs for a long time and I have no idea what caused it to stop working all of the sudden, but running Set-ExecutionPolicy RemoteSigned -Scope CurrentUser command fixed it for me. I was able to nvs rm latest and nvs add latest to get it working again.

dizlexik avatar Oct 21 '18 03:10 dizlexik

Thanks for the explanation!

I will look into signing the nvs PowerShell script, which I think should avoid this problem.

jasongin avatar Oct 22 '18 17:10 jasongin

@StarpTech and @dizlexik do you know what your execution policy was before you changed it? If it was AllSigned, then signing the script would have helped. But not if it was Restricted; maybe in that case the installer should block with a message.

jasongin avatar Oct 22 '18 17:10 jasongin

I feel stupid for not checking before I changed it :/

I had never modified it, though, so I would imagine that it was probably the default of Restricted.

I just checked on another PC where nvs is working fine and my execution policy is Restricted. Strange.

dizlexik avatar Oct 22 '18 17:10 dizlexik

FWIW, I just did a fresh install via the .msi on a brand new out of the box Surface Laptop 2 with Windows 10 Home Edition and hit this same issue. I don't recall seeing any error messages from the installer, but I might have missed it.

Running: Set-ExecutionPolicy RemoteSigned in admin PowerShell fixed the issue.

I've not hit this issue on many other machines I've installed nvs on, but usually those machines have had other things done to them first, which usually means tweaking that setting as well.

christopheranderson avatar Jan 21 '19 19:01 christopheranderson

@jasongin according to the docs Restricted is the default policy.

We should add it at least to the readme and for better dev experience we should try to add it to the installer.

StarpTech avatar Jan 22 '19 09:01 StarpTech

The MSI installer does not use PowerShell to add the nvs directory to your PATH. So that part should work regardless of PowerShell execution policy.

While the installer does invoke the nvs.ps1 script, the purpose of that is to download and cache the node binary. That is optional at install time and can be done automatically at first-run instead, however that works regardless of PowerShell execution policy anyway. The reason is the installer actually invokes nvs.cmd, which then runs powershell.exe -ExecutionPolicy Unrestricted nvs.ps1.

I ran this test just now: with my PowerShell execution policy set to Restricted, I installed nvs.msi. It successfully added the AppData\Local\nvs directory to my path, and also downloaded node.exe to the nvs\cache directory. Then I opened a new PowerShell window and typed nvs. The result was:

nvs : File C:\Users\jasongin\AppData\Local\nvs\nvs.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

That's a reasonably clear error message. Then as soon as you manually change the execution policy, it works.

Is that different from what you saw?

Re-reading the original issue reported by @StarpTech, I think at step 3 "Run nvs script from app directory directly", nvs.cmd was run instead of nvs.ps1. But running the .cmd script from a PowerShell window does not properly modify the PATH. You need to either run the .ps1 script from PowerShell or the .cmd script from a Windows command shell -- and normally that's what happens if you just type nvs from any other directory. Maybe nvs.cmd should detect and block when invoked from a PowerShell window, if that is even possible?

jasongin avatar Jan 22 '19 18:01 jasongin

I'm having this issue on the latest version 1.7.1. And setting ExecutionPolicy to RemoteSigned didn't make a difference! I even reinstalled nvs.

xc6pm avatar Jan 06 '24 10:01 xc6pm

@ebram-h - Could you please share the error message, just to confirm it's the same issue, and share the output of Get-Command nvs and Get-ExecutionPolicy?

christopheranderson avatar Jan 06 '24 18:01 christopheranderson