9

I would like to know if its possible to write an powershell script that connects to an Server via ssh and then do something on that server.

Thank you for your help.

3
  • That is a very abstract question. the answer is that you can make a call via ssh, and run a (non-interactive) command via powershell/batch file, but you need a third party SSH client AFAIK Commented Oct 28, 2018 at 18:55
  • Concret I have a raspberry its used for streaming music. There are two softwares installed. One for Apple (Airplay) and on is just for simulating a Spotify connect Device. I need a script that connects to the raspberry via ssh. It the should increase the volume with alsamixer. Commented Oct 28, 2018 at 18:57
  • 1
    Sure, you need a ssh client you can use on the command line, some are available, most prominent is the openSSH client, but also PuTTY comes with a command line executable. You can call that executable from your script and specify an option that allows to specify a script to be executed on the remote site. However you can not execute a powershell script on the server side, since that most likely is a Linux system, so unixoid. An ssh server on a MS-Windows systems is totally exotic and you can use powershell only on that system, other operating systems offer far better shell solutions. Commented Oct 28, 2018 at 18:58

2 Answers 2

2

There are many articles all over the web on this topic, and since PowerShell Core is now Open Source and can be installed on Windows / Linux / OSX the SSH for PowerShell has been an thing for a while now.

Example(s):

Using SSH to Access Linux Servers in PowerShell

Using SSH with PowerShell

Managing Windows Powershell from Linux terminal

There a several module on the MS PowerShellGallery specifically for this use case.

Find-Module -Name '*ssh*'

Version              Name                                Repository           Description                                                                                  
-------              ----                                ----------           -----------                                                                                  
2.0.2                Posh-SSH                            PSGallery            Provide SSH and SCP functionality for executing commands against remote hosts.               
2.1.3                SSHSessions                         PSGallery            Svendsen Tech's SSH-Sessions module provides SSH session creation, management and interact...
0.0.2.0              OpenSSHUtils                        PSGallery            Utilities and functions for configuring OpenSSH on Windows.                                  
1.0.0                SSH                                 PSGallery            Provides a PowerShell-based SSH client based on SSH.net  http://sshnet.codeplex.com/         
1.1.3                PowerSSH                            PSGallery            This module detects the first use of an SSH command, automatically runs the SSH agent, kee...
0.9.4                WinSSH                              PSGallery            Install OpenSSH-Win64, optionally install ssh-agent and sshd Services. Also includes funct...
0.0.30               PSSharedGoods                       PSGallery            Module covering functions that are shared within multiple projects                           
1.0.1                ssh-wrapper                         PSGallery            Exposes ssh from WSL by wrapping: bash -c "ssh $args". Requires Windows Subsystem for Linu...
1.0.4                PSShortcut                          PSGallery            This module eases working with Windows shortcuts (LNK and URL) files.                        
1.0                  cEPRSSharepoint                     PSGallery            DSCModule helps in installing & configuring the sharepoint site, Farm etc.,                  
2.0.1.8              SkypeForBusinessHybridHealth        PSGallery            Uses on-premises modules such as Skype For Business and SkypeOnlineConnector to validate b...
0.3.1                posh-sshell                         PSGallery            Provides integration with ssh-agent and pageant from within Powershell                       
1.1.4                PowerSSH-Legacy                     PSGallery            This module detects the first use of an SSH command, automatically runs the SSH agent, kee...

SSH From Windows Server to Linux Server - Invoke-SSHCommand

Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750& sleep 2; kill $!"

# Results

Host       : LinuxServerA
Output     : {}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750& sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750 2>&1 & sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {* About to connect() to WindowsServerA port 4750, *   Trying 10.10.10.10... connected, * Connected to
             WindowsServerA (10.10.10.10) port 4750}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750 2>&1 & sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {* About to connect() to LinuxServerB port 4750, *   Trying 10.10.10.11... connected, * Connected to
             LinuxServerB (10.10.10.11) port 4750}
ExitStatus : 0
Sign up to request clarification or add additional context in comments.

Comments

0

I was about to suggest that you use PuTTY. in fact, you should be using plink

If you look at SSH (the command for Linux), you can execute remote commands in this fashion:

Single command:

ssh $HOST ls

Several commands:

ssh $HOST ls; pwd; cat /path/to/remote/file

If you want to do this on a windows machine, you either need to use plink.exe or a similar tool.

Apparently there is also some library for powershell to execute remote commands via ssh, but this is the result of me spending 30 seconds to google remote command ssh powershell so I do not know if it will work for you, or if it requires powershell on the remote (you can get it for linux)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.