0

如果你想在 Windows 的 PowerShell 上以管理员身份运行命令,你可以这样写:

Start-Process foo -Verb RunAs -ArgumentList '...'

但这在 Linux 上是不可能的。Powershell 会返回以下错误:Start-Process: The parameter '-Verb' is not supported for the cmdlet 'Start-Process' on this edition of PowerShell.确实,根据Powershell 在线文档,“ [ - Verb]参数不适用于非 Windows 系统”,因此无法在 Linux 上像这样以管理员身份运行 Powershell。

我的问题很简单:如何在 Linux 上提升 Powershell 权限?如何sudo ...在 Linux 上使用 Powershell 模拟 BASH?我知道你可以用 bash 和类似的东西来解决这个问题sudo pwsh -Command '...',但我正在寻找一种“100% PowerShell”的方法来做到这一点。

提前感谢您的回复。

PS:“PowerShell”是指最新的“PowerShell 7”。

4

2 回答 2

0

使用 sudo pwsh -c:

PS> (sudo pwsh { gci -r /opt/tomcat/ .sh | ? name -like 'startup ' }).fullname /opt/tomcat/bin/startup.sh

于 2022-02-01T21:01:00.733 回答
0

没有powershell-ish办法这样做,因为elevation功能取决于操作系统。在 Windows 和 Linux 中,您实际上在不同的安全上下文(以及在单独的用户会话下)启动了一个新进程,但在 Windows 中,有内置的提升机制使用verbs. Linux 不支持动词,你必须使用sudoor su

如果你再深入一点,Start-Process -Verb在 Windows 下创建适当[System.Diagnostics.ProcessStartInfo].Verb填充并使用[System.Diagnostics.Process]::Start($ProcessStartInfo).

因此,在 Linux 下,除了设置之外,您在内部具有相同的设置.Verb,而是将旧值设置FileNamesudo并将旧值移动到参数。

就像没有任何通用的方法可以同时驾驶自行车和宇宙飞船。

于 2021-02-16T18:25:31.080 回答