首页 > 解决方案 > 如何在 Linux 上使用 Powershell(提升 Powershell,以管理员身份运行命令)“sudo”?

问题描述

如果你想在 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”。

标签: linuxpowershelladminpowershell-corepowershell-7.0

解决方案


使用 sudo pwsh -c:

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


推荐阅读