Getty Images

Tip

These Posh-SSH examples pave the way to Linux management

Need to work with remote Linux systems? A PowerShell module with file-handling functionality can help Windows shops looking for a reliable management platform.

While PowerShell is now a cross-platform project that runs on Windows, Linux and Mac systems, the management tool still needs a helping hand to work across these different OSes.

More Linux workloads continue to take up more infrastructure workloads traditionally held by Windows Server, such as Active Directory domain controllers. However, the management tools in Windows tend to be just that, and the spectrum of available tools may not support Linux. It's easy enough to use PowerShell to get information from Window Server systems, but despite PowerShell's cross-platform abilities, this work has always been more difficult with Linux servers. Rather than learning Bash scripting, administrators can tap their PowerShell skills to write code to gather information from Linux machines. A PowerShell module named Posh-SSH helps to bridge this administrative divide with a set of cmdlets to provide additional functionality to work with Linux systems.

Install the Posh-SSH module

To start, install the module in Linux with the following command with administrator rights:

Install-Module -Name Posh-SSH

Depending on the configuration of the workstation, the following command that modifies the execution policy might be required if commands do not output as expected:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Verbose

The Posh-SSH module includes several functions that will be familiar to Linux administrators, such as the ability to pass commands via a Secure Shell (SSH) session. To get a complete list of Posh-SSH commands use the following in PowerShell:

Get-Command -Module Posh-SSH

To run commands against a Linux server via Posh-SSH, the administrator needs to do two things: connect to an SSH-enabled Linux server and then invoke the requisite Posh-SSH module.

After that, using Invoke-SSHCommand will return the results of the command into a variable. This gives administrators a useful way to gather information about files, their contents or even running processes.

To use these features, OpenSSH must be running on the server in question. Test this by using PuTTY or a similar tool to connect on port 22.

Connect to the Linux machine

To start, establish a session with a Linux VM with OpenSSH installed. In its raw form, the command is the following:

$setsession = New-SSHSession

This prompts the administrator to fill in several fields. When it's time to automate, a script can fill in the appropriate properties. Substitute the computername and Get-Credential username for your own setup requirements:

$setsession = New-SSHSession -ComputerName "192.168.83.128" -Credential (Get-Credential sysadmin)

The first time the command connects to the Linux server, it prompts to accept the host's fingerprint. Press Y to approve it.

connect to Linux VM
Connect to a Linux session from Windows PowerShell.

Another option is to use SSH keys that are passed as a private key file. The Get-Help New-SSHSession command shows the choices.

This tutorial uses the Get-Credential function for the sake of simplicity. To specify the credentials in a script, replace the Get-Credential portion.

Interrogate the system

For a connected session, use Invoke-Session with the command in single quotes as shown below:

$Results = Invoke-SSHCommand -SessionId 0 -Command 'uname -a'

This returns the full details of the uname -a command as if it had been typed in the console; uname -a is the Linux command to output all system information. Posh-SSH supports several open sessions at once; just substitute the SessionId variable for the appropriate session. To see the active sessions, use the Get-SSHSession command.

Linux server command
Execute commands on a Linux server through the Posh-SSH PowerShell cmdlets.

The data returned comes back as object; to get the desired output, use the Select-Object cmdlet and the Output property:

$Results|Select-Object Output

While simple, these Posh-SSH examples show how a more advanced script can gather information about Linux hosts to build a report with a wide range of data, including memory usage and running processes.

Use Posh-SSH to upload files to Linux servers

Administrators can use Posh-SSH for important infrastructure jobs, such as uploading files to a group of Linux servers to pre-stage updates, rather than manually uploading each file.

The Posh-SSH module supports several file transfer options, including SSH FTP and Secure Copy Protocol (SCP). The following Posh-SSH example uploads a file with the SCP command and only requires OpenSSH server functionality.

Set-SCPItem -computername 192.168.83.128 -Credential  (Get-Credential sysadmin) .\update.zip – Destination /tmp/

The Posh-SSH module helps to extend the reach of PowerShell into the Linux arena. Windows admins who are new to managing these systems will have to learn about a few of the fundamental differences between the two OSes, such as how to work file paths and being aware of case sensitivity on the Linux side.

More information about the Posh-SSH module can be found at its repository on GitHub.

Dig Deeper on Microsoft messaging and collaboration