简体   繁体   中英

How to "sudo" with Powershell (elevate Powershell, running a command as admin) on Linux?

If you want to run a command as admin on PowerShell on Windows, you can write something like this:

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

But it is not possible on Linux. Powershell will return you the following error: Start-Process: The parameter '-Verb' is not supported for the cmdlet 'Start-Process' on this edition of PowerShell. Indeed, According to the Powershell online documentation , " the [-Verb] parameter does not apply for non-Windows systems ", so it is not possible to run Powershell as admin like this on Linux.

My question is simple: how to elevate Powershell privileges on Linux? How can I simulate BASH's sudo... with Powershell on Linux? I know that you can make a workaround with bash and some thing like sudo pwsh -Command '...' , but I am looking for a "100% PowerShell" way to do this.

Thanks in advance for your responses.

PS: By "PowerShell", I mean "PowerShell 7", the latest one.

There is no powershell-ish way of doing this, because elevation feature depends on OS. In both Windows and Linux you actually start a new process under a different security context (and under separate user session), but in Windows there is built-in elevation mechanism using verbs . Linux does not support verbs, and you have to use sudo or su .

If you go little bit deeper, Start-Process -Verb under Windows creates appropriate [System.Diagnostics.ProcessStartInfo] with .Verb filled and runs process using [System.Diagnostics.Process]::Start($ProcessStartInfo) .

So, under Linux you internally have the same except instead of setting .Verb , you set FileName to sudo and move old value to arguments.

Like there is no any universal method to drive both bicycle and spaceship.

Use sudo pwsh -c:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM