0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Abdullah - i will give it a try and let you know how it went.
I've downloaded and extracted those files in the C:\ directory and this is what i get: Add-Type : Cannot add type. The ".BIN" extension is not supported. At line:1 char:9 + Add-Type <<<< -Path C:\Tools\SharpSSH-1.1.1.13.bin + CategoryInfo : InvalidArgument: (.BIN:String) [Add-Type], Exception + FullyQualifiedErrorId : EXTENSION_NOT_SUPPORTED,Microsoft.PowerShell.Commands.AddTypeCommand Any other suggestiions?
SharpSSH-1.1.1.13.bin is not the Library. Try extracting zip further, Tamir.SharpSSH.dll is the file you need to import.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.