Search code examples
linuxpowershellsudoplink

use powershell and plink to shutdown remote linux computer with sudo command


I'm attempting to shutdown a RHEL7 linux computer remotely from a windows computer in a non-interactive fashion using powershell and plink.

I can send commands and retrieve the result like so:

$plinkPath = "C:\somePath"
$ipAddress = "192.168.111.1"
$username = "userName"
$password = "password"
$resultPath = "C:\somewhere"
$resultFile = "someFile.txt"
& "$plinkPath\plink.exe" @("$ipAddress", "-l", "$username", "-pw", "$password", "(pwd)" | Out-File "$resultPath\$resultFile"

The command above prints the working directory in linux and saves it to file. However, I can't send a shutdown command like this because the command must be run as root on this computer. I believe there should be some syntax like the below to run a sudo command in a similar way:

& "$plinkPath\plink.exe" @("$ipAddress", "-l", "$username", "-pw", "$password", "(sudo ... shutdown -h now)") | Out-File "$resultPath\$resultFile"

Is there some way to send a shutdown command with sudo using this method?

Thanks.


Solution

  • Make sure that the user can indeed run sudo commands. The issue might be that the user that is trying to run the command is requested to insert the sudo password. In the sudoers file insert <YOUR_USERNAME> ALL=(ALL) NOPASSWD