使用powershell和plink通过sudo命令关闭远程linux计算机

问题描述

我正在尝试使用 powershell 和 plink 以非交互式方式从 Windows 计算机远程关闭 RHEL7 linux 计算机。

我可以像这样发送命令并检索结果:

$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"

上面的命令打印linux中的工作目录并将其保存到文件中。但是,我无法发送这样的关闭命令,因为该命令必须在这台计算机上以 root 身份运行。我相信应该有一些像下面这样的语法来以类似的方式运行 sudo 命令:

& "$plinkPath\plink.exe" @("$ipAddress","(sudo ... shutdown -h Now)") | Out-File "$resultPath\$resultFile"

有什么方法可以使用这种方法通过 sudo 发送关闭命令吗?

谢谢。

解决方法

确保用户确实可以运行 sudo 命令。 问题可能是尝试运行命令的用户被要求插入 sudo 密码。 在 sudoers 文件中插入 <YOUR_USERNAME> ALL=(ALL) NOPASSWD