user4211329
user4211329

Reputation:

Powershell Script to SSH to unix server

what im trying to do is write a powershell script to: *SSH into a unix server *run sudo su - username - this will then ask to enter your password *then execute a script that's already in the unix server. *then close

I have close to no experience writing PS scrips - can anyone point me in the right direction so i can plug this things together?

Thanks in advance fellas.

Upvotes: 0

Views: 2475

Answers (1)

Abdullah Leghari
Abdullah Leghari

Reputation: 2470

You can invoke the command line tool plink.exe from within PowerShell which requires PuTTY to be installed, and difficult to use.

PowerShell's biggest strength probably is its ability to use .Net Libraries. SharpSSH is a .Net implementation of SSH2 protocol and could be used from within PowerShell.

Download SharpSSH, extract the zip and load the SharpSSH assembly by writing following line of code on top of your PowerShell script.

Add-Type -Path C:\SharpSSH.dll #assumes you extracted SharpSSH in C:\

A new SSH session could be created as,

New-SshSession user1 example.org # You'll be prompted for a password
if( Invoke-Ssh who '\$'| select-string "^(?!user1).*pts/\d"  ) {
   Invoke-Ssh "shutdown -r 0:00"  '\$'
}
else {
   Invoke-Ssh "shutdown -r now"  '\$'
}
Remove-SshSession

Have a look at this and this example Scripts for invoking various other SSH commands from within PowerShell.

Upvotes: 1

Related Questions