PowerShell 上的提升/Sudo

Elevate / Sudo on PowerShell

我发现了一些 PowerShell 提升/sudo 函数,但其​​中 none 似乎运行良好(以 "as intuitively and seamlessly as on every Unix and Linux distribution" 方式)。它们大多是多余的,因为它们不能很好地工作。如果有人在 PowerShell 上拥有无缝工作的 elevate / sudo,他们就会知道。

我看到的功能问题是:

• 它们只能通过调用 powershell.exe 的另一个实例来使用外部脚本。即如果你想做一些像 sudo gcisudo Get-ChildItem 这样简单的事情会产生错误,因为这些方法似乎不喜欢调用别名或 Cmdlet(出于某种原因!)。

• 您无法将现有的控制台会话无缝提升到管理员,这似乎需要提升/sudo 功能打开一个全新的控制台(似乎很麻烦,必须白白打开一个新控制台!)?

有人使用可靠的 elevate / sudo 吗?我不希望它是完美的,如果有充分的技术原因导致上面的事情不起作用(可能与 PowerShell 主机本身的能力不足有关)那么那很好,但它会很好知道我们可以在 PowerShell 中使用功能提升/sudo 走多远。令人遗憾的是,尽管 PowerShell 比 bash 先进得多(而且它的对象操作功能也超过了 Python 和 Perl imo),但有时它似乎是 Unix 中一些最简单的功能-土地,像 sudo,吹走 PowerShell 中的可能 - 我很乐意看到这些空白被填补,以便 PowerShell 可以显示出与 Unix 一样的能力(甚至更多!)进行更改。

当然,盒子里没有原生的东西,所以,apples/oranges 将 sudo 的东西与 Windows 进行比较。

安全性 boundaries/functionalities 只是不同,众所周知,Windows(以及 PowerShell)中的 sudo 等价物是 RunAs,它将弹出 Windows UAC,没有得到围绕这一点,无需关闭 UAC(不要这样做)或设置 AppCompat shim。

所以,当你说功能时,你是说你已经厌倦了这些:

Find-Module -Name '*sudo*' | 
Select Name, Version, Type, Description

# Results
<#
Name   Version Type   Description                                                                            
----   ------- ----   -----------                                                                            
Sudo   2.1.0   Module Use functionality similar to sudo in PowerShell. GitHub: https://github.com/pldmgg/Sudo
PSSudo 1.4.0   Module Function for executing programs with adminstrative privileges 
#>

这类问题在这里出现了很多,并且已经回答了好几次。所以,你是说,你试过下面的方法吗?

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

Sudo !! equivalent in PowerShell

runas /user:domain\administrator $^

Is there any 'sudo' command for Windows?

doskey sudo= runas /user:Administrator "cmd /k cd \"%cd%\" & $*"

runas /noprofile /user:Administrator cmd 

另请参阅:

Support sudo #3232

5 Windows Alternatives to the Linux sudo Command

gsudo 是 Windows 的 sudo,其行为类似于 Unix sudo(在当前控制台 windows 中提升命令或 cmd/ps shell) .它在 Powershell 中工作,但有局限性:提升的内存 space 不能与非提升的内存共享对象,因此不能共享变量,并且必须对对象进行某种编组完成。目前 gsudo 做的是最幼稚但至少诚实的编组:只有字符串可以来回传递。 您可以将字符串文字与需要提升到 gsudo 的命令一起传递。然后gsudoreturns一个可以抓取的字符串,不是powershell对象。

# Commands without () or quotes  
PS C:\> gsudo Remove-Item ProtectedFile.txt
or
PS C:\> gsudo 'Remove-Item ProtectedFile.txt'

# On strings enclosed in single quotation marks ('), escape " with \"
$hash = gsudo '(Get-FileHash \"C:\My Secret.txt\").Hash'
# For variable substitutions, use double-quoted strings with single-quotation marks inside
$hash = gsudo "(Get-FileHash '$file' -Algorithm $algorithm).Hash"
# or escape " with \""
$hash = gsudo "(Get-FileHash \""$file\"" -Algorithm $algorithm).Hash"

# Test gsudo success (optional):
if ($LastExitCode -eq 999 ) {
    'gsudo failed to elevate!'
} elseif ($LastExitCode) {
    'Command failed!'
} else { 'Success!' }

或者,您可以在 Powershell:

中调用 gsudo 提升电流 shell
PS C:\> gsudo
(Accept UAC popup)
PS (ADMIN) C:\> Remove-Item ProtectedFile.txt
PS (ADMIN) C:\> exit
PS C:\>