Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh using powerShell script by passing the password along with the Commnad

Tags:

powershell

ssh

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

like image 332
issac john Avatar asked Dec 11 '22 03:12

issac john


1 Answers

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.

like image 70
Amit Baranes Avatar answered Mar 23 '23 01:03

Amit Baranes