im able to ssh into a server from PowerShell if i type
ssh username@host
This prompts a password and i enter the password which works
But im trying to write a script which ssh into a server executes some scripts and get back
so i have to pass the password along with the command how do i do this
there are lot of answers for shell script how do i do this in PowerShell
I have tried
echo "password" | ssh user@host
doesn't seem to work
You can work with POSH-SSH module.
Code snippet:
$Password = "Password"
$User = "UserName"
$ComputerName = "Destination"
$Command = "SSH Command"
$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
UPDATE:
3 years later, Microsoft publishes PowerShell remoting over SSH
.
More info can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With