ssh使用powerShell脚本通过密码和Command一起传递

问题描述 投票:0回答:1

如果我键入,我能够从PowerShell ssh到服务器

ssh username @ host

这会提示输入密码,然后输入有效的密码

但我试图编写一个ssh到服务器的脚本执行一些脚本并返回

所以我必须传递密码和命令我该怎么做

shell脚本有很多答案我如何在PowerShell中执行此操作

我试过了

echo“密码”| ssh user @ host

似乎不起作用

powershell ssh
1个回答
1
投票

你可以使用POSH-SSH module

代码段:

$Password = "Password"
$User = "UserName"
$ComputerName = "Destination"

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH

Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command # Invoke Command Over SSH
© www.soinside.com 2019 - 2024. All rights reserved.