简体   繁体   English

Visual Studio代码终端,如何以管理员权限运行命令?

[英]Visual studio code terminal, how to run a command with administrator rights?

The new version 1.2.0 include a terminal, but when I try to install any pack with node I get the npm ERR.新版本 1.2.0 包含一个终端,但是当我尝试使用节点安装任何包时,我得到 npm ERR。 code EPERM that I usually solve right clicking and running it as administrator?我通常解决右键单击并以管理员身份运行的代码EPERM? So how I do that in the vscode terminal?那么我如何在 vscode 终端中做到这一点? There is something like sudo for linux? linux有类似sudo的东西吗?

vscode 终端

Option 1 - Easier & Persistent选项 1 - 更容易和持久

Running Visual Studio Code as Administrator should do the trick.以管理员身份运行 Visual Studio Code 应该可以解决问题。

If you're on Windows you can:如果您在 Windows 上,您可以:

  1. Right click the shortcut or app/exe右键单击快捷方式或应用程序/exe
  2. Go to properties转到属性
  3. Compatibility tab兼容性选项卡
  4. Check "Run this program as an administrator"勾选“以管理员身份运行此程序”
There is a caveat to it though 不过有一个警告

Make sure you have all other instances of VS Code closed and then try to run as Administrator.确保您已关闭所有其他 VS Code 实例,然后尝试以管理员身份运行。 The electron framework likes to stall processes when closing them so it's best to check your task manager and kill the remaining processes.电子框架喜欢在关闭进程时停止进程,所以最好检查你的任务管理器并杀死剩余的进程。

Related Changes in Codebase 代码库中的相关更改

Option 2 - More like Sudo选项 2 - 更像 Sudo

If for some weird reason this is not running your commands as an Administrator you can try the runas command.如果由于某种奇怪的原因这没有以管理员身份运行您的命令,您可以尝试runas命令。 Microsoft: runas command 微软:runas 命令

Examples 例子
  • runas /user:Administrator myCommand
  • runas "/user:First Last" "my command"
Notes 笔记
  • Just don't forget to put double quotes around anything that has a space in it.只是不要忘记在任何有空格的地方加上双引号。
  • Also it's quite possible that you have never set the password on the Administrator account, as it will ask you for the password when trying to run the command.此外,您很可能从未在管理员帐户上设置密码,因为它会在尝试运行命令时要求您输入密码。 You can always use an account without the username of Administrator if it has administrator access rights/permissions.如果帐户具有管理员访问权限/权限,您始终可以使用没有管理员用户名的帐户。

Step 1: Restart VS Code as an adminstrator第 1 步:以管理员身份重新启动 VS Code

(click the windows key, search for "Visual Studio Code", right click, and you'll see the administrator option) (点击windows键,搜索“Visual Studio Code”,右键,会看到管理员选项)

Step 2: In your VS code powershell terminal run Set-ExecutionPolicy Unrestricted第 2 步:在您的 VS 代码 powershell 终端中运行Set-ExecutionPolicy Unrestricted

In my case even while running as admin I had to set the execution policy.就我而言,即使以管理员身份运行,我也必须设置执行策略。

In the terminal type:在终端类型中:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

Running as admin didn't help me.以管理员身份运行对我没有帮助。 (also got errors with syscall: rename) (系统调用也有错误:重命名)

Turns out this error can also occur if files are locked by Windows.事实证明,如果文件被 Windows 锁定,也会发生此错误。

This can occur if :如果出现以下情况,可能会发生这种情况:

  • You are actually running the project您实际上正在运行该项目
  • You have files open in both Visual Studio and VSCode.您在 Visual Studio 和 VSCode 中都打开了文件。

Running as admin doesn't get around windows file locking.以管理员身份运行不会绕过 Windows 文件锁定。

I created a new project in VS2017 and then switched to VSCode to try to add more packages.我在 VS2017 中创建了一个新项目,然后切换到 VSCode 尝试添加更多包。 After stopping the project from running and closing VS2017 it was able to complete without error停止项目运行并关闭 VS2017 后,它能够完成而没有错误

Disclaimer: I'm not exactly sure if this means running as admin isn't necessary, but try to avoid it if possible to avoid the possibility of some rogue package doing stuff it isn't meant to.免责声明:我不确定这是否意味着不需要以管理员身份运行,但如果可能的话,尽量避免它,以避免某些流氓包做不该做的事情的可能性。

Here's what I get.这就是我得到的。

I'm using Visual Studio Code and its Terminal to execute the 'npm' commands.我正在使用 Visual Studio Code 及其终端来执行“npm”命令。

Visual Studio Code (not as administrator) Visual Studio Code(不是管理员)
PS g:\labs\myproject> npm install bootstrap@3 PS g:\labs\myproject> npm install bootstrap@3

Results in scandir and/or permission errors.导致 scandir 和/或权限错误。

Visual Studio Code (as Administrator) Visual Studio Code(以管理员身份)
Run this command after I've run something like 'ng serve'在我运行类似 'ng serve' 之后运行这个命令

PS g:\labs\myproject> npm install bootstrap@3 PS g:\labs\myproject> npm install bootstrap@3

Results in scandir and/or permission errors.导致 scandir 和/或权限错误。

Visual Studio Code (as Administrator - closing and opening the IDE) Visual Studio Code(以管理员身份 - 关闭和打开 IDE)
If I have already executed other commands that would impact node modules I decided to try closing Visual Studio Code first, opening it up as Administrator then running the command:如果我已经执行了其他会影响节点模块的命令,我决定先尝试关闭 Visual Studio Code,以管理员身份打开它,然后运行命令:

PS g:\labs\myproject> npm install bootstrap@3 PS g:\labs\myproject> npm install bootstrap@3

Result I get then is: + bootstrap@3.3.7我得到的结果是: + bootstrap@3.3.7
added 115 packages and updated 1 package in 24.685s在 24.685 秒内添加了 115 个包并更新了 1 个包

This is not a permanent solution since I don't want to continue closing down VS Code every time I want to execute an npm command, but it did resolve the issue to a point.这不是一个永久的解决方案,因为我不想每次执行 npm 命令时都继续关闭 VS Code,但它确实在一定程度上解决了问题。

There are two ways you can solve this problem,有两种方法可以解决这个问题,

For temporary对于临时
You can follow these steps for it,您可以按照以下步骤操作,

  1. Search Vscode after in the windows.在windows中搜索Vscode。
  2. Then right-click and run the VScode as an administrator.然后右键单击并以管理员身份运行 VScode。

But you can do this every time when you open the VScode.So I supposed it is not the best way to solve this problem.但是每次打开VScode时都可以这样做。所以我认为这不是解决这个问题的最佳方法。

Best way最好的办法
You can follow these steps for it,您可以按照以下步骤操作,

  1. Search Vscode after in the windows.在windows中搜索Vscode。
  2. Then right-click and click the open file location.然后右键单击并单击打开的文件位置。
  3. Then you can click the VScode icon and go to the properties.然后您可以单击 VScode 图标并转到属性。
  4. Next you want to go to the compatibility tab.接下来,您要转到兼容性选项卡。
  5. At the bottom of the tab, there are selection boxes, in there you can see run this program as an administrator selection box you want to tick it.在选项卡的底部,有选择框,在那里您可以看到以管理员身份运行此程序选择框,您要勾选它。
  6. Then you can apply those changes and save it.然后您可以应用这些更改并保存它。

You can refer to below page to do more things related to this run as administrator stuffs您可以参考以下页面以管理员身份执行与此运行相关的更多操作
https://www.sevenforums.com/tutorials/11841-run-administrator.html https://www.sevenforums.com/tutorials/11841-run-administrator.html

The only thing that solved the issue for me唯一为我解决问题的东西

Right click on Vs Code exe file -> Compatibility -> Check "Run this program as administrator"右键单击Vs Code exe文件->兼容性->选中“以管理员身份运行此程序”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制应用程序以管理员权限运行 - How to force an application to run with Administrator rights 如何在没有管理员权限的情况下运行 Heroku CLI? - How to run Heroku CLI without administrator rights? 如何从C ++代码创建具有管理员权限的服务 - How to create a service with administrator rights from c++ code 在 Windows 11 上运行没有管理员权限的 Visual Studio 2022 - Run Visual Studio 2022 without admin rights on Windows 11 作为 Windows 10 管理员,如何从 Python 以管理员身份运行命令? - As a Windows 10 administrator, how to run a command as administrator from Python? Visual Studio 2019 始终从任务栏图标以管理员身份运行 - Visual Studio 2019 always run as administrator from the task bar icon 如何以管理员身份在 python 中运行 cmd 命令? - How to run a cmd command in python as an administrator? 为什么我的 Visual Studio 解决方案可执行文件需要以管理员身份运行? - Why do my Visual Studio solution executables need to run as Administrator? 无法使用管理员帐户运行 Visual Studio 2008 - Unable to run Visual Studio 2008 using Administrator account Visual Studio Code 集成终端中的错误变音字母与 PowerShell 在运行和运行选择之间 - Wrong diacritic letters in Integrated Terminal of Visual Studio Code with PowerShell between Run and Run Selection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM