Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system

元气小坏坏 提交于 2020-05-22 11:26:06

问题


Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:

File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.

After reading this I tried to run the visual studio code in administrator mode, thinking that the problem was a matter of privileges. But the error is throwing anyway.


回答1:


I found out here that you can add to your visual studio code settings the following and the problem will vanish:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

The reason is that, in build command line integrations like visual studio code, you need to set the command line policies by your self. By setting the above configurations, the visual studio code will do that for you.

(read this to understand better the command line policies)




回答2:


For more simplicity, I want to add the vs code settings path in addition to Ricardo's answer. you can get it like this:

File -> Preferences -> Settings and in the search bar write "automation".

After that, by looking your operating system enter "edit in settings.json".

Finally, add the following b/n the braces:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]



回答3:


Face the same issue and this works for me. open PowerShell as Administrator and paste this.

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser  



回答4:


I opened VS Code as Administrator and ran this command in the terminal:

Set-ExecutionPolicy Unrestricted

It allowed me to run the scripts without an errors.




回答5:


I have faced the same problem due to security reasons on Windows 10. I managed to fix it by running the following command in PowerShell:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted



回答6:


Run VS-Code as ADMIN.

This fixed my issue on my home PC with running ng new my-App which had produced this error when I first opened VS-Code from the installation process.




回答7:


If you don't need to explicitly run this on PowerShell, then you may want to try running it in the command prompt.

Just type cmd and press enter. Doing so will open the command prompt in the current terminal window. It will behave same as a normal command prompt.




回答8:


This is not a VSCode specific issue, it is a OS one. There are different levels for machine, user, process that allow PowerShell Scripts to run and it must be enabled.

If this is your machine just change the execution policy to RemoteSigned or unrestricted so that is applies to your entire system. If you just want to use it in VSCode, the change your settings file or change you shortcut to include the execution policy you want to use. The Recommendation is RemoteSigned. Meaning all local code will run, but any remote scripts must be signed.

If this is a corporate machine, that has the Execution set to Restricted, then no code will run until that setting is changed. You can still run code by selecting it all in the VSCode editor and hitting F8. Now, if they have enforced this policy, then you need to talk to the IT time to get them to change it for you.

All of this is detailed in the PowerShell Help files.

Get-Command -Name '*executionpolicy*' | ft -AutoSize

CommandType Name                Version Source                       
----------- ----                ------- ------                       
Cmdlet      Get-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet      Set-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security


# get function / cmdlet details
(Get-Command -Name Get-ExecutionPolicy).Parameters
Get-help -Name Get-ExecutionPolicy -Full
Get-help -Name Get-ExecutionPolicy -Online
Get-help -Name Get-ExecutionPolicy -Examples

    Get-ExecutionPolicy
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    Get-ExecutionPolicy -List

(Get-Command -Name Set-ExecutionPolicy).Parameters
Set-help -Name Set-ExecutionPolicy -Full
Set-help -Name Set-ExecutionPolicy -Online
Set-help -Name Set-ExecutionPolicy -Examples

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    Set-ExecutionPolicy -ExecutionPolicy Restricted
    Invoke-Command -ComputerName "Server01" -ScriptBlock {Get-ExecutionPolicy} | Set-ExecutionPolicy -Force
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy AllSigned -Force
    Get-ExecutionPolicy -List
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy AllSigned
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Note, if your org is controlling this, the again, talk to them before trying to bypass the policy, because if they did this, this also means they are monitoring it, and if you do this that will know. I have seen things like this engender RPE's. (Resume Producing Events)




回答9:


The simplest way to deal with this problem from Visual Studio Code is to change from powerShell to cmd (assuming there's no reason for using powershell in your case). To do so, follow these few steps:

  1. Terminal -> New Terminal
  2. In right lower corner select "Select Default Shell" option

  1. Select "Command prompt"

I have not found such solution among current answers. Hope that helps!



来源:https://stackoverflow.com/questions/56199111/visual-studio-code-cmd-error-cannot-be-loaded-because-running-scripts-is-disabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!