如何在 Windows 上的 powershell 上执行 sudo [英] How to sudo on powershell on Windows

查看:324
本文介绍了如何在 Windows 上的 powershell 上执行 sudo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我需要运行 powershell 脚本时,它都会抱怨安全性,如果我添加 powershell.exe -nologo -executionpolicy bypass -File .\install.ps1 我仍然会得到权限被拒绝的未授权访问异常.我只想运行这个安装脚本,sudo 相当于在 windows 上的 powershell 上键入什么?

Whenever I need to run a powershell script it complains of security, if I add powershell.exe -nologo -executionpolicy bypass -File .\install.ps1 I still get permission denied unauthorizedAccessException. I just want to run this install script, what is the sudo equivalent to type on the powershell on windows?

推荐答案

注意:如果您想添加通用预打包 sudo 类似于 PowerShell 的功能,请考虑
Enter-AdminPSSession (psa) 函数来自 this Gist,在这个答案的底部讨论.

Note: If you're looking to add general-purpose, prepackaged sudo-like functionality to PowerShell, consider the
Enter-AdminPSSession (psa) function from this Gist, discussed in the bottom section of this answer.

如果您已经从 PowerShell 运行,则使用 Start-Process -Verb RunAs 如下:

Start-Process -Verb RunAs powershell.exe -Args "-executionpolicy bypass -command Set-Location \`"$PWD\`"; .\install.ps1"

注意:

  • 脚本总是在新窗口中运行.
  • 由于新窗口的工作目录总是 $env:windir\System32Set-Location 调用会切换到调用者的工作目录 ($PWD) 是前置的.
    • 请注意,在 PowerShell (Core) 7+ (pwsh.exe) 中不再需要这样做,因为调用者的当前位置是继承的.
    • The script invariably runs in a new window.
    • Since the new window's working directory is invariably $env:windir\System32, a Set-Location call that switches to the caller's working directory ($PWD) is prepended.
      • Note that in PowerShell (Core) 7+ (pwsh.exe) this is no longer necessary, because the caller's current location is inherited.
      • 从好的方面来说,这消除了对 -nologo 的需求.
      • 一个一般性的警告是 -Command 可以改变传递给脚本的参数的解释方式(在您的情况下没有),因为它们的解释方式与您通过时的解释方式相同PowerShell 中的参数,而 -File 将它们视为文字.
      • On the plus side, this obviates the need for -nologo.
      • A general caveat is that -Command can change the way arguments passed to your script are interpreted (there are none in your case), because they are interpreted the same way they would be if you passed the arguments from within PowerShell, whereas -File treats them as literals.

      如果您从 PowerShell 外部调用,通常是从 cmd.exe/批处理文件,您需要将上述内容包装在对powershell.exe的外部调用中,不幸的是,这使引用方面的事情变得复杂:

      If you're calling from outside of PowerShell, typically from cmd.exe/ a batch file, you need to wrap the above in an outer call to powershell.exe, which complicates things in terms of quoting, unfortunately:

      powershell.exe -command "Start-Process -Verb RunAs powershell.exe -Args '-executionpolicy bypass -command', \"Set-Location `\"$PWD`\"; .\install.ps1\""
      


      交互式,当然,您可以:

      • 右键单击 PowerShell 快捷方式(在您的任务栏或开始菜单中,或在您的桌面上),选择 以管理员身份运行 以打开以管理员权限运行的 PowerShell 窗口,然后运行.\install.ps1 从那里开始.

      • Right-click the PowerShell shortcut (in your taskbar or Start Menu, or on your Desktop), select Run as Administrator to open a PowerShell window that runs with admin privileges, and run .\install.ps1 from there.

      或者,从现有的 PowerShell 窗口,您可以使用 Start-Process -Verb RunAs powershell.exe 打开以管理员身份运行的窗口,如 AdminOfThings 的回答.

      Alternatively, from an existing PowerShell window, you can open a run-as-admin window with Start-Process -Verb RunAs powershell.exe, as in AdminOfThings' answer.

      这篇关于如何在 Windows 上的 powershell 上执行 sudo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆