Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system
Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system
在 visual studio 代码中,我试图从命令行执行 script.bat,但出现以下错误:
File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.
阅读this后,我尝试运行管理员模式下的visual studio代码,认为问题是权限问题。但是无论如何都会抛出错误。
我发现 here that you can add to your visual studio code settings 以下问题将消失:
对于 visual studio 代码设置,转到文件 -> 首选项 -> 设置 -> 扩展 -> 向下滚动并找到“在 settings.json 中编辑”。 不要忘记重启visual studio代码
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
原因是,在像visual studio代码这样的构建命令行集成中,您需要自己设置命令行策略。通过设置上述配置,visual studio 代码将为您完成。
(阅读 this 以更好地理解命令行策略)
2021 年 8 月更新
似乎 terminal.integrated.shellArgs
已弃用。 答案显示了如何在新版本中做到这一点。
这不是 VSCode 的具体问题,而是 OS 的问题。 机器、用户、进程有不同级别允许 PowerShell 脚本 运行 并且必须启用它。
如果这是您的机器,只需将执行策略更改为 RemoteSigned 或不受限制,以便应用于您的整个系统。如果您只想在 VSCode 中使用它,请更改您的设置文件或更改您的快捷方式以包含您要使用的执行策略。建议是 RemoteSigned。这意味着所有本地代码都将 运行,但任何远程脚本都必须签名。
如果这是一台公司计算机,其执行设置为受限,则在更改该设置之前不会有任何代码 运行。您仍然可以 运行 通过在 VSCode 编辑器中全选并按 F8 来编写代码。现在,如果他们强制执行了此政策,那么您需要与 IT 部门沟通,让他们为您更改政策。
PowerShell 帮助文件中详细介绍了所有这些内容。
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
请注意,如果您的组织正在控制它,请再次与他们交谈 before trying to bypass the policy,因为如果他们这样做了,这也意味着他们正在监视它,如果您这样做,他们就会知道。我已经看到像这样的事情会产生 RPE。 (继续制作活动)
运行 VS-Code 作为管理员。
这解决了我在家用 PC 上使用 运行 ng new my-App 的问题,当我在安装过程中首次打开 VS-Code 时产生了这个错误。
为了更简单,我想在里卡多的答案之外添加vs代码设置路径。你可以这样得到它:
文件 -> 首选项 -> 设置,然后在搜索栏中输入“自动化”。
之后,通过查看您的操作系统输入“在 settings.json 中编辑”。
最后,添加如下b/n大括号:
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
出于安全原因,我在 Windows 10 上遇到了同样的问题。
我设法通过 运行 PowerShell 中的以下命令修复了它:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
我以管理员身份打开了 VS Code,运行 在终端中输入了这个命令:
Set-ExecutionPolicy Unrestricted
它让我 运行 脚本没有错误。
如果您不需要在 PowerShell 上显式 运行 此项,那么您可能想尝试 运行 在命令提示符中启用它。
只需输入 cmd
并按回车键。这样做将在当前终端 window 中打开命令提示符。它的行为与普通命令提示符相同。
遇到同样的问题,这对我有用。
以管理员身份打开 PowerShell 并粘贴它。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
从 Visual Studio 代码处理此问题的最简单方法是从 powerShell 更改为 cmd(假设在您的情况下没有理由使用 powershell)。为此,请按照以下几个步骤操作:
- 终端 -> 新终端
- 右下角select"Select Default Shell"选项
- Select "Command prompt"
我目前的答案中没有找到这样的解决方案。希望对您有所帮助!
打开 VS 代码
文件 -> 首选项 -> 设置
在搜索栏中 -> 搜索“terminal.integrated.shellArgs”
点击终端的“在setting.json中编辑”>集成>ShellArgs>windows[无论你OS,select各自的设置]
在 json 文件的顶部添加:
terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "绕过"]
例如,添加后 json 可能如下所示:
{
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"],
"git.autofetch": true,
"peacock.favoriteColors": [
{
"name": "Angular Red",
"value": "#b52e31"
},
{
"name": "Auth0 Orange",
"value": "#eb5424"
},
- 重新启动 VS 代码并再次尝试命令(在我的例子中,我正在尝试 ng serve)
注意:terminal.integrated.shellArgs 设置已被弃用。
出于安全原因,如果您与其他用户共享计算机,请使用:
PowerShell:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
不需要管理员权限。
我的 visual studio 2019(版本 16.8.4)出现了这个错误。
最后我找到了解决方案:
EnableScripts(来自 PowerShell os windows(来自 windows 10 的注册表编辑器)
如下所示浏览 PowerShell 并使 0 => 1 就这样。
解决问题的步骤,
- 在您的计算机中打开 PowerShell(这将更改您 Windows 计算机上的 PowerShell 执行策略)
- 运行(为您的机器获取执行策略)
Get-ExecutionPolicy -List
- 运行
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
运行执行以下命令即可解决问题。
set-ExecutionPolicy RemoteSigned -Scope CurrentUser
(我只在 windows 中尝试过)
按键盘上的 windows 键
类型'PowerShell'
右键单击 Windows PowerShell
以管理员身份单击运行
运行以下命令并用‘Y’确认
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
更多信息:https://www.roelpeters.be/solved-running-scripts-is-disabled-on-this-system-in-powershell/
问题是 PowerShell 默认限制 运行 脚本,除非我们更改 execution policies.
我们可以通过添加 -ExecutionPolicy Bypass
参数来更改执行策略。在 vscode 中,您必须通过在 settings.json 中添加以下内容来使用这些参数创建一个 shell 配置文件(ctrl + shift + p 并键入“settings.json”)
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
需要重启 VS Code 和终端。
注意:terminal.integrated.shellArgs
设置已 deprecated。
- 打开 VS 代码
- 转到文件 -> 首选项 -> 设置
- 在搜索栏中 -> 搜索“terminal.integrated.defaultprofile.windows”
- 点击终端的“在 setting.json 中编辑”> 集成 > 默认配置文件:Windows [无论您 OS、select 各自的设置]。
在 JSON 文件的顶部添加:
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": [
"-ExecutionPolicy",
"Bypass"
]
}
},
注意:以下设置行已弃用:
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
对于 Win10 用户
- 在Visual Studio代码中:
File -> Preferences -> Settings
- 输入搜索:
terminal.integrated.defaultProfile.windows
- 点击
Edit in setting.json
- 添加这个:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"path" : [
"${env:windir}\System32\WindowsPowerShell\v1.0\powershell.exe"
],
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-NoLogo",
"-ExecutionPolicy",
"ByPass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
- 保存并重启Visual Studio代码
撕掉我的头发后,这就是我最终做的。我没有使用 CMD 或 PowerShell,而是切换到 BASH。
在 settings.json:
//"terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
// "terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe",
"terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe",
此处的 CMD 和 Powershell 已被注释掉,以防您想返回其中之一。
要到达 settings.json:
> File -> Preferences -> Settings -> in search type : "settings.json" ->
> Under [JSON] click "Edit in settings.json"
在 settings.json
中添加了这一行
"terminal.integrated.defaultProfile.windows": "PowerShell"
我在 VS powershell 中使用了以下命令,它开始工作了
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
标签位置已更改,步骤:
按 Ctrl+Shift+P 并键入 'settings',然后选择 'Preferences: Open settings (JSON)':
然后在 settings.json 中的 "terminal.integrated.profiles.windows"."PowerShell" 中添加 "args": ["-ExecutionPolicy", "Bypass"] :
重启 VS 代码。
如果将来 VS 代码更新有任何变化,可能会有不同的步骤。
几分钟前我遇到了同样的问题:
我这样做了,它对我有用
您需要编辑 visual studio 设置。
转到文件 -> 首选项 -> 设置 -> 扩展 -> 向下滚动并找到“在 settings.json 中编辑”。通过下面的这些代码,保存并重新启动 visual studio 代码。
{
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
"files.associations": {
"*.html": "html"
},
}
添加此错误消息只是为了将来可以轻松找到此主题:
ng : File C:\Users\[USER_NAME]\AppData\Roaming\npm\ng.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.
At line:1 char:1
+ ng help
+ ~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
在 visual studio 代码中,我试图从命令行执行 script.bat,但出现以下错误:
File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.
阅读this后,我尝试运行管理员模式下的visual studio代码,认为问题是权限问题。但是无论如何都会抛出错误。
我发现 here that you can add to your visual studio code settings 以下问题将消失: 对于 visual studio 代码设置,转到文件 -> 首选项 -> 设置 -> 扩展 -> 向下滚动并找到“在 settings.json 中编辑”。 不要忘记重启visual studio代码
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
原因是,在像visual studio代码这样的构建命令行集成中,您需要自己设置命令行策略。通过设置上述配置,visual studio 代码将为您完成。
(阅读 this 以更好地理解命令行策略)
2021 年 8 月更新
似乎 terminal.integrated.shellArgs
已弃用。
这不是 VSCode 的具体问题,而是 OS 的问题。 机器、用户、进程有不同级别允许 PowerShell 脚本 运行 并且必须启用它。
如果这是您的机器,只需将执行策略更改为 RemoteSigned 或不受限制,以便应用于您的整个系统。如果您只想在 VSCode 中使用它,请更改您的设置文件或更改您的快捷方式以包含您要使用的执行策略。建议是 RemoteSigned。这意味着所有本地代码都将 运行,但任何远程脚本都必须签名。
如果这是一台公司计算机,其执行设置为受限,则在更改该设置之前不会有任何代码 运行。您仍然可以 运行 通过在 VSCode 编辑器中全选并按 F8 来编写代码。现在,如果他们强制执行了此政策,那么您需要与 IT 部门沟通,让他们为您更改政策。
PowerShell 帮助文件中详细介绍了所有这些内容。
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
请注意,如果您的组织正在控制它,请再次与他们交谈 before trying to bypass the policy,因为如果他们这样做了,这也意味着他们正在监视它,如果您这样做,他们就会知道。我已经看到像这样的事情会产生 RPE。 (继续制作活动)
运行 VS-Code 作为管理员。
这解决了我在家用 PC 上使用 运行 ng new my-App 的问题,当我在安装过程中首次打开 VS-Code 时产生了这个错误。
为了更简单,我想在里卡多的答案之外添加vs代码设置路径。你可以这样得到它:
文件 -> 首选项 -> 设置,然后在搜索栏中输入“自动化”。
之后,通过查看您的操作系统输入“在 settings.json 中编辑”。
最后,添加如下b/n大括号:
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
出于安全原因,我在 Windows 10 上遇到了同样的问题。 我设法通过 运行 PowerShell 中的以下命令修复了它:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
我以管理员身份打开了 VS Code,运行 在终端中输入了这个命令:
Set-ExecutionPolicy Unrestricted
它让我 运行 脚本没有错误。
如果您不需要在 PowerShell 上显式 运行 此项,那么您可能想尝试 运行 在命令提示符中启用它。
只需输入 cmd
并按回车键。这样做将在当前终端 window 中打开命令提示符。它的行为与普通命令提示符相同。
遇到同样的问题,这对我有用。 以管理员身份打开 PowerShell 并粘贴它。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
从 Visual Studio 代码处理此问题的最简单方法是从 powerShell 更改为 cmd(假设在您的情况下没有理由使用 powershell)。为此,请按照以下几个步骤操作:
- 终端 -> 新终端
- 右下角select"Select Default Shell"选项
- Select "Command prompt"
我目前的答案中没有找到这样的解决方案。希望对您有所帮助!
打开 VS 代码
文件 -> 首选项 -> 设置
在搜索栏中 -> 搜索“terminal.integrated.shellArgs”
点击终端的“在setting.json中编辑”>集成>ShellArgs>windows[无论你OS,select各自的设置]
在 json 文件的顶部添加:
terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "绕过"]
例如,添加后 json 可能如下所示:
{ "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"], "git.autofetch": true, "peacock.favoriteColors": [ { "name": "Angular Red", "value": "#b52e31" }, { "name": "Auth0 Orange", "value": "#eb5424" },
- 重新启动 VS 代码并再次尝试命令(在我的例子中,我正在尝试 ng serve)
注意:terminal.integrated.shellArgs 设置已被弃用。
出于安全原因,如果您与其他用户共享计算机,请使用: PowerShell:
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
不需要管理员权限。
我的 visual studio 2019(版本 16.8.4)出现了这个错误。 最后我找到了解决方案: EnableScripts(来自 PowerShell os windows(来自 windows 10 的注册表编辑器)
如下所示浏览 PowerShell 并使 0 => 1 就这样。
解决问题的步骤,
- 在您的计算机中打开 PowerShell(这将更改您 Windows 计算机上的 PowerShell 执行策略)
- 运行(为您的机器获取执行策略)
Get-ExecutionPolicy -List
- 运行
Set-ExecutionPolicy -Scope CurrentUser Unrestricted
运行执行以下命令即可解决问题。
set-ExecutionPolicy RemoteSigned -Scope CurrentUser
(我只在 windows 中尝试过)
按键盘上的 windows 键
类型'PowerShell'
右键单击 Windows PowerShell
以管理员身份单击运行
运行以下命令并用‘Y’确认
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
更多信息:https://www.roelpeters.be/solved-running-scripts-is-disabled-on-this-system-in-powershell/
问题是 PowerShell 默认限制 运行 脚本,除非我们更改 execution policies.
我们可以通过添加 -ExecutionPolicy Bypass
参数来更改执行策略。在 vscode 中,您必须通过在 settings.json 中添加以下内容来使用这些参数创建一个 shell 配置文件(ctrl + shift + p 并键入“settings.json”)
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
需要重启 VS Code 和终端。
注意:terminal.integrated.shellArgs
设置已 deprecated。
- 打开 VS 代码
- 转到文件 -> 首选项 -> 设置
- 在搜索栏中 -> 搜索“terminal.integrated.defaultprofile.windows”
- 点击终端的“在 setting.json 中编辑”> 集成 > 默认配置文件:Windows [无论您 OS、select 各自的设置]。
在 JSON 文件的顶部添加:
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": [
"-ExecutionPolicy",
"Bypass"
]
}
},
注意:以下设置行已弃用:
"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
对于 Win10 用户
- 在Visual Studio代码中:
File -> Preferences -> Settings
- 输入搜索:
terminal.integrated.defaultProfile.windows
- 点击
Edit in setting.json
- 添加这个:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"path" : [
"${env:windir}\System32\WindowsPowerShell\v1.0\powershell.exe"
],
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-NoLogo",
"-ExecutionPolicy",
"ByPass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
- 保存并重启Visual Studio代码
撕掉我的头发后,这就是我最终做的。我没有使用 CMD 或 PowerShell,而是切换到 BASH。 在 settings.json:
//"terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
// "terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe",
"terminal.integrated.shell.windows": "C:\Program Files\Git\bin\bash.exe",
此处的 CMD 和 Powershell 已被注释掉,以防您想返回其中之一。 要到达 settings.json:
> File -> Preferences -> Settings -> in search type : "settings.json" ->
> Under [JSON] click "Edit in settings.json"
在 settings.json
中添加了这一行"terminal.integrated.defaultProfile.windows": "PowerShell"
我在 VS powershell 中使用了以下命令,它开始工作了
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
标签位置已更改,步骤:
按 Ctrl+Shift+P 并键入 'settings',然后选择 'Preferences: Open settings (JSON)':
然后在 settings.json 中的 "terminal.integrated.profiles.windows"."PowerShell" 中添加 "args": ["-ExecutionPolicy", "Bypass"] :
重启 VS 代码。
如果将来 VS 代码更新有任何变化,可能会有不同的步骤。
几分钟前我遇到了同样的问题: 我这样做了,它对我有用
您需要编辑 visual studio 设置。 转到文件 -> 首选项 -> 设置 -> 扩展 -> 向下滚动并找到“在 settings.json 中编辑”。通过下面的这些代码,保存并重新启动 visual studio 代码。
{
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": ["-ExecutionPolicy", "Bypass"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
"files.associations": {
"*.html": "html"
},
}
添加此错误消息只是为了将来可以轻松找到此主题:
ng : File C:\Users\[USER_NAME]\AppData\Roaming\npm\ng.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.
At line:1 char:1
+ ng help
+ ~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess