Using SSH to Access Linux Servers in PowerShell

A question I’ve fielded now and again in the past, “Can I use PowerShell to access Linux servers?”. Among others, there were a few answers I could give of varying degrees of usefulness depending on the requirements:

I was recently asked this again at my current workplace and discovered a project I hadn’t seen previously, a PowerShell module based on the SSH.NET library.

Once you have downloaded and imported the module, check out what is available:

Get-Command -Module SSH-Sessions

SSH1

To work with a Linux server, first of all you need to establish a session to the server with New-SshSession (I think this cmdlet would benefit from a Credential parameter):

New-SshSession -ComputerName PuppetVM -Username root -Password puppet

SSH2

Examine our connected sessions:

Get-SshSession | Format-Table -AutoSize

SSH3

 

It’s possible to now enter an interactive session with this VM and run some commands, for example to look at the OS and disk space:

Enter-SshSession -ComputerName PuppetVM

cat /proc/version

df -h

exit

SSH4

 

Similar to Invoke-Command in Windows you can use Invoke-SshCommand to send commands to an established session and receive results back (Note: you can use the -Quiet parameter if you don’t wish to see the display on screen):

$Query = Invoke-SshCommand -ComputerName PuppetVM -Command "ls /root"

SSH5

We can now work with these results from the $Query variable. What we get back looks like multiple strings, but is actually an object with a (long) single string

$Query.GetType()

$Query.Count

$Query[0].GetType()

SSH6

However, we can work with these a bit easier, should we need to, by breaking them up into individual strings:

$Strings = $Query[0] -split "`n"

$Strings

$Strings.Count

SSH7a

So far I’ve found this module pretty useful. There are few drawbacks I’ve found so far, including some limitations with ESXi 5.0 and above which are mentioned on the web page, but I hope this project will continue to be updated further.

 

27 thoughts on “Using SSH to Access Linux Servers in PowerShell

  1. You can actually manage these from Windows (System Center Virtual Machine Manager), so I wonder if this is using PowerShell in the background or maybe just VMware’s API?

  2. If I wanted to do the following:

    Invoke-SshCommand -ComputerName $I -Command “sudo echo ‘rwcommunity somestring-rw 100.100.100.100’ >>/etc/snmp/snmpd.conf”

    and it is giving a permission denied error because of the “>>” how would you escape these characters?
    \>\> does not give the desired affect and deither does ‘>’>

    Thanks,

    Jared

  3. Nice piece of functionality; thanks. I miss/wish it was possible to paste text from (other) windows into the session; or is there perhaps a way to configure this?

    @Jared: Try to scape your >> symbols the powershell way, i.e. using backticks: `>`> . I have a password with a ” symbol and had to write it that way

  4. Hi,
    I’m using the SSH-Sessions Module for running the commands on the remote Linux Machine with CentOS-OpenLogic. But it is throwing “sudo: sorry, you must have a tty to run sudo”. But when i try the same command from putty it is working.
    My code goes like this:
    New-SshSessions -ComputerName -Username -Keyfile
    Invoke-SshCommand -ComputerName -Command “sudo mkdir testfolder”

    Remove-Sshsession

    The same code is working for Ubuntu Server 13.10, but it is not working for CentOS-OpenLogic.

    Anyone please help me on this
    Bhaskar.D

  5. One issue with the script:
    When I enter the session and try to go to root (/) directory, it does not do that.
    It stays on /root home directory of root user.

    PS C:\temp> Enter-SshSession -ComputerName 192.18.1.100
    [192.18.1.100]: /root # : df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/vg_snmplin64-lv_root 18G 6.1G 11G 38% /
    tmpfs 1.5G 76K 1.5G 1% /dev/shm
    /dev/sda1 485M 104M 356M 23% /boot
    [192.18.1.100]: /root # : cd /

    [192.18.1.100]: /root # : ls
    anaconda-ks.cfg
    Desktop
    Documents
    Downloads
    imros
    imroz
    install.log
    install.log.syslog
    Music
    Pictures
    Public
    Templates
    Videos
    [192.18.1.100]: /root # : cd /

    [192.18.1.100]: /root # : ls
    anaconda-ks.cfg
    Desktop
    Documents
    Downloads
    imros
    imroz
    install.log
    install.log.syslog
    Music
    Pictures
    Public
    Templates
    Videos

  6. Hi, I am using SSH.NET to run SSH commands against our HMC server I am using this command:

    Result lshwres -r proc -m –level lpar -F lpar_name,lpar_id,curr_procs,run_procs

    However the result is type Object not Array.

    When I print $result[0] I get 1 character ..its not doing it strings 🙁

  7. I am getting this error while trying to connect to a linux server using the New-SShSession cmdlet.

    This is the error:

    Unable to connect to [Comptuer Name]: Exception calling “Connect” with “0” argument(s): “A non-recoverable error occurred during a database lookup”

  8. thanks for the information but I want to build a cmdlet usimg the c# which does this work of making a connection. if you know can you please help. thanks in advance

  9. Did you try access Cisco or SonicWALL with same method? When I try performance a command after sucess connection I receive this message “An established connection was aborted by the server”.

  10. So this just puts the output of the command, rather than the active SSH like Putty.

    I SSH to a server that then SSHes into another server due to ACL restrictions.

    When I run any commands, it waits for output to show up.

    Example:
    Ping 8.8.8.8
    = no output, as it is still running, and never stops – can’t CTRL+C out of it either

    ping 8.8.8.8 -w 4
    = ping output of 4 count, doesn’t populate until the entire output is released.

    Is there something that I am missing, or am I going back to Putty / waiting for Windows to actually release an SSH cmdlet?

  11. Hello,
    I downloaded SSH-Sessions by Jonathan link. But when I try ssh connection, i’ve this error :

    Unable to create SSH client object for X.X.X.X: The type [Renci.SshNet.Ssh Client] was not found: Check that the assembly in which it is located is responsible.

    But I can’t solve the problem, can you help me?

  12. Thanks for your reponse. But I resolved this problem. I installed powershell 3. And Now it’s good !

  13. I’m trying to logon usign sudo su but the PS session just sits there and does nothing.
    I get a successfully connected when I run New-SshSession but nothing when I then run
    Invoke-SshCommand -ComputerName mycomputer -Command “sudo su -”
    I’m connecting to Ubuntu 10.04.4 LTS .
    Any ideas, suggestions greatfully received.

  14. How to convert unix script into power sheel script any automation tool or manual if so how steps can any one reply please.

    Fakrudeen

  15. Unable to create SSH client object for ‘xx.xx.xx.xxx’: Method not fo
    und: ‘System.Management.Automation.PSArgumentException System.Management.Automation.PS
    TraceSource.NewArgumentException(System.String, System.String, System.Object[])’.
    iam facing this error while trying to connect linux server using SSH module please help me out in solving this .Thanks in advance

Comments are closed.