如何在 Windows 上的 powershell 上执行 sudo

How to sudo on powershell on Windows

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

您可以使用 运行 作为管理员选项启动 PowerShell:

Start-Process powershell -Verb runAs

如果您有阻止脚本执行的公司政策,那么可以。 ByPass 不会更改您的配置文件(用户上下文)状态。这不是任何这些开关的设计(用例)regarding Execution Policies

Windows中没有直接比较sudo,这与PowerShell无关。您要么是会话/应用程序的管理员,要么不是。如果您要安装软件,则意味着您必须是管理员。如果您正在进行全局系统范围的更改,则意味着您必须是管理员。

有些人努力实现脚本、包装函数和/或模块来模仿 sudo …

来自 MS PowerShell 库的模块。 Sudo 0.9.3 在 PowerShell 中使用类似于 sudo 的功能

来自GitHub Sudo for PowerShell

Sudo for PowerShell Installation From PowerShell, create a $profile if you don't have one:

if (!(test-path $profile)) { new-item -path $profile -itemtype file -force }

Open the profile in notepad:

notepad.exe $profile

Add the following line and save the file:

. /path/to/sudo.ps1

sudo will be available in all new PowerShell windows Usage

sudo application [arguments ...]

...但这不会改变 Windows 在处理安全边界时的预期。

另请参阅此问答 Sudo !! equivalent in PowerShell

$^ is a variable that expands to the last executed Powershell command. You can run a command as another user using runas, so the following works:

runas /user:domain\administrator $^

To shorten that up a bit, you can do some magic with aliases. Take a look at this Technet article for more info.

EDIT: One caveat - $^ only executes the first command in a pipeline or multi-command line. If you need to redo an entire command that is peppered with pipes or semicolons, use Invoke-History instead (which defaults to the last full command in its entirety).

注意:如果您要添加通用预打包 sudo 类似 PowerShell 的功能,请考虑
Enter-AdminPSSession (psa) 来自 this Gist, discussed in the bottom section of 的函数。

如果您已经运行宁来自PowerShell,那么使用Start-Process -Verb RunAs如下:

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

注:

  • 脚本总是 运行s 在 new window.
  • 由于新的 window 的工作目录总是 $env:windir\System32,因此会预先设置一个 Set-Location 调用,该调用切换到调用者的工作目录 ($PWD)。
    • 请注意,在 PowerShell (Core) 7+ (pwsh.exe) 中,这不再是必需的,因为调用者的当前位置是继承的。
  • 执行 Set-Location 需要使用 -Command 而不是 -File
    • 从好的方面来说,这消除了 -nologo.
    • 的需要
    • 一个普遍的警告是 -Command 可以改变传递给脚本的参数的解释方式(在你的情况下有 none),因为它们的解释方式与如果您从 PowerShell 中传递了参数,而 -File 将它们视为文字。

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

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

互动,当然可以:

  • 右键单击 PowerShell 快捷方式(在您的任务栏或“开始”菜单中,或在您的桌面上),select Run as Administrator 打开一个 PowerShell window 运行s 具有管理员权限,运行 .\install.ps1 从那里。

  • 或者,从现有的 PowerShell window,您可以使用 Start-Process -Verb RunAs powershell.exe 打开 运行-as-admin window,如 .

可以使用Start-Process命令,然后使用参数-Verb runas进行提升。这对于启动提升进程非常有用。

我创建了一个这样的 sudo 函数并将其添加到我的 powershell 配置文件中:

function sudo {
    Start-Process @args -verb runas
}

示例:以管理员身份打开记事本以编辑主机文件

sudo notepad C:\Windows\System32\drivers\etc\hosts

如果你想提升一个 Powershell 命令,你可以像这样创建一个简单的函数:

function Start-ElevatedPS {
    param([ScriptBlock]$code)

    Start-Process -FilePath powershell.exe -Verb RunAs -ArgumentList $code
}

然后,调用函数并传递包裹在{}(脚本块)

中的命令

示例:提升以创建符号 link

Start-ElevatedPS { New-Item -ItemType SymbolicLink -Name mySymlink.ps1 -Target C:\myTarget.ps1 }

如果您使用的是 Chocolatey(包管理器),则可以安装名为 sudo.
的包 然后你可以像 Linux 这样使用 sudo

截至今天(2021 年 10 月),winget install gerardog.gsudo 成功了(在 windows 10 家庭版上)。编辑:也在 Windows 11 测试(2022 年 4 月)

之后,您可以这样做:

gsudo notepad C:\windows\system32\something-editable-by-admin-only.txt

要测试它是否有效,或者在您的情况下:

gsudo powershell.exe install.ps1

windows` UAC 会提示您通过 gsudo 提升您的权限,您可以在此处阅读源代码:https://github.com/gerardog/gsudo