PowerShell – Linux and OSx – Passing Get-Credential to Sudo

If you want to run a PowerShell CmdLet that requires elevation on Linux or OSx from a non elevated PowerShell session, you might need to use sudo.

The three lines below allows you to pass PowerShell’s Get-Credential to sudo, which will elevate the next PowerShell CmdLet you want to run.

get-credential_sudo

Note: You have to invoke a new PowerShell session to elevate the CmdLet as PowerShell is run in memory.

If you want to assign the results of a CmdLet that was elevated with sudo to a variable the results are returned as a string and not an object. The reason is not sudo but how PowerShell returns its results back (This will include data written to any stream like verbose etc).

sudoStringBack

See link: https://github.com/PowerShell/PowerShell/blob/master/docs/KNOWNISSUES.md#sudo-exec-and-powershell

About the author

Ben Taylor

Cloud and PowerShell enthusiast with a penchant for automation and CI.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *