First, PowerShell Core have to be installed on every target Linux machine and added to its /etc/ssh/sshd_config
:
Probably, in a script you have the remote session to that target box started like:
If you have to execute a sudo command in that session, you could do like:
Imagine you put your script into CI / CD pipeline and want to see its output in a log. In case of failure of a command invoked like shown above, probably, you’ll be frustrated with the error output like this:
Such useless output originate from the fact that fail happens in a remote session. You can’t cope with that by just adding a conversion of an output error to exception with -ErrorAction Stop
parameter for Invoke-Expression
, because the output of the invoked shell command is not a PowerShell error output.
But throwing exceptions is the right way of passing errors. So, the working solution includes writing output to a file and throwing its content with the exception this way:
Perfect, but has too many lines of code for just copying and pasting it all over the script. So, let’s introduce a function:
Using this function you can invoke sudo commands with a single line of code:
Script variables can also be used:
PowerShell allows you to wrap things in a pretty neat way, isn’t it?
Guessing can you use this function inside of a 1st level remote session to execute the sudo commands in the nested remote sessions? Sure, export a function to the remote session and invoke it there. Look in this repository for the implementation and usage of Export-FunctionRemote
.