Although PowerShell is now a cross-platform project that runs on Windows, Linux, and Mac systems, the management tool still needs a helping hand to run on these various operating systems.
More and more Linux workloads continue to occupy more infrastructure workloads traditionally owned by Windows Server, such as Active Directory domain controllers. However, Windows management tools tend to be just that, and the range of tools available may not support Linux. It’s easy enough to use PowerShell to get information from Windows Server systems, but despite the cross-platform capabilities of PowerShell, this job has always been more difficult with Linux servers. Rather than learning Bash scripts, administrators can use their PowerShell skills to write code to collect information from Linux machines. A PowerShell module named Posh-SSH helps bridge this administrative divide with a set of cmdlets to provide additional functionality work with linux systems.
Install the Posh-SSH module
To start, install the module on Linux with the following command with administrator rights:
Install-Module -Name Posh-SSH
Depending on the workstation configuration, the following command that changes the execution policy may be required if the commands do not display 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 place orders through a Secure Shell (SSH) session. For a complete list of Posh-SSH commands, use the following in PowerShell:
Get-Command -Module Posh-SSH
To run commands on a Linux server through Posh-SSH, the administrator needs to do two things: connect to an SSH-capable Linux server, and then invoke the required Posh-SSH module.
After that, using Invoke-SSHCommand will return the results of the command in 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 to port 22.
Connect to the Linux machine
To get started, establish a session with a Linux virtual machine with OpenSSH installed. In its raw form, the command is:
$setsession = New-SSHSession
This prompts the administrator to fill in multiple fields. When it’s time to automate, a script can fill in the appropriate properties. Replace the Computer name and Get-Credential username for your own configuration 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 fingerprint. hurry Yes to approve it.
Another option is use SSH keys which are passed as a private key file. The Get help New SSHS session command displays the choices.
This tutorial uses the Obtain credentials function for the sake of simplicity. To specify credentials in a script, replace the Obtain credentials portion.
Query the system
For a connected session, use Summon-Session with the command in single quotes as shown below:
$Results = Invoke-SSHCommand -SessionId 0 -Command 'uname -a'
This returns all the details of the uname -a command as if it had been typed into the console; uname -a is the linux command to display all system information. Posh-SSH supports multiple open sessions at a time; just replace the Session ID variable for the appropriate session. To view active sessions, use the Get-SSHSession order.
The returned data arrives return as subject; to obtain the desired output, use the Select-Object cmdlet and the Go out goods:
Although simple, these Posh-SSH examples show how a more advanced script can collect information about Linux hosts to report with a wide range of data, including memory usage and running process.
Use Posh-SSH to upload files to Linux servers
Administrators can use Posh-SSH for important infrastructure tasks, such as uploading files to a group of Linux servers for preliminary 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 downloads a file with the SCP command and only requires the OpenSSH server functionality.
Set-SCPItem -computername 192.168.83.128 -Credential (Get-Credential sysadmin) .update.zip – Destination /tmp/
The Posh-SSH module helps extend the reach of PowerShell in the Linux arena. Windows administrators who are new to managing these systems will need to familiarize themselves with some of the basic differences between the two operating systems, such as the way file paths work and the case sensitivity of the Linux side.
You can find more information on the Posh-SSH module at its repository on GitHub.