PowerShell - Use POSH-SSH or similar to run remote SUDO command on Linux with SSH

Viewed 108

I am trying to do a, for my feeling, really simple task for these last two days. I want to automate my home environment with PowerShell for a project and I want to auto update an Ubuntu 20.04 host and run a few other SUDO commands. I have used SSHSessions and now I am trying POSHSSH. With my searching and trying I have gotten so far to be able to run commands that don't require SUDO, but that's obviously needed for an apt update && apt upgrade.

Write-Host "Put in credentials for the Docker host" -Verbose
$dockercreds = Get-Credential

$OpenedSession = New-SSHSession -ComputerName 192.168.1.8 -Credential $dockercreds
$sID = $OpenedSession | Select-Object -ExpandProperty SessionId
$SSHOut = (Invoke-SSHCommand -Session $sID -Verbose -Command "sudo apt upgrade -y").Output
$SSHOut

Remove-SSHSession -SessionId $sID

Stuff like pwd works and gives a brief output,

Put in credentials for the Docker host
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
/home/dockeruser
True

but when I run something like sudo apt update it's empty and didn't work.

Put in credentials for the Docker host
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:

True

I did find some stuff about securesessionstream or similar, but I couldn't figure it out or find much documentation. Can someone help me with this? It shouldn't be this hard, right?

0 Answers
Related