Insight and analysis on the information technology space from industry thought leaders.

Linux + PowerShell = Awesome, Not Awkward, Together!

Have you ever thought of managing your Linux systems via PowerShell? This guide will let you know how you can invoke SSH commands via PowerShell.

Veeam Guest Blogger

December 15, 2016

3 Min Read
Linux + PowerShell = Awesome, Not Awkward, Together!

How many of you have to deal with both Windows and Linux machines at the same time? I guess quite a few. And, wouldn’t it be cool if you could manage both from a single PowerShell console? In this post, I’ll show you how to manage your Linux system via PowerShell by invoking SSH commands. It can be any Linux machine: physical, virtual or one in the public cloud.

Adding SSH functionality to PowerShell

While we are still waiting for Microsoft to add SSH support to Windows, we can leverage the tool called Posh-SSH. All you need to add it to PowerShell is the command:

Install-Module Posh-SSH

Once you have added SSH functionality, check your available commands:

# Get the list of available commandsGet-Command -Module Posh-SSH

You’ll get a whole list of commands, but, before using them, let’s connect to our Linux machine by creating a new SSH session:

# Open new SSH sessionNew-SSHSession -ComputerName 10.0.0.196 -Credential (Get-Credential)

Invoking SSH commands

Now, as a proof of concept, let’s try something simple: getting a machine’s hostname. As a side note: In all of the following commands, I’m going to add Format-List -Property Output to hide all of the sections except for the output.

Invoke-SSHCommand -Index 0 -Command "hostname" | Format-List -Property Output

As you can see, our Linux machine has just returned us its hostname! So, now that we have proven that this is working, let’s try using some applications. I’m going to show it on the example of a free version of Veeam Agent for Linux.

Using Linux applications

I’m going to use

veeamconfig

This is a command that launches the configuration of Veeam Agent for Linux. This is what you see in Linux shell when you use it:

Now, to invoke this command to our connected Linux machine via PowerShell, I’ll use:

Invoke-SSHCommand -Index 0 -Command "veeamconfig" | Format-List -Property Output

 

As you can see on the screenshot, the length of the output is very limited and most of it is cut. But for the sake of the demo, let’s get the full output. To do that, I’ll apply a command that removes the string length limit and try again:

# Remove the output truncation$FormatEnumerationLimit=-1Invoke-SSHCommand -Index 0 -Command "veeamconfig" | Format-List -Property Output

 

Now the output is not truncated, and we can see all of the text, but it still doesn’t look like it does in native Linux shell. Thus, we need to apply some formatting. In this particular case, I need to remove several spaces in a row and make line breaks:

$string = Invoke-SSHCommand -Index 0 -Command "veeamconfig" | Format-List -Property Output | Out-String(Write-Output $string).Replace("     ", "").Replace("`n", "").Split(",")

 

This looks pretty similar to what we are looking for. Not perfect, but it’s much more readable.

What’s next?

So this proof of concept works pretty well. You can invoke the SSH commands directly to your Linux machine and even get the appropriate output if you apply some string formatting. I asked my fellow IT pros if they had already thought of combining PowerShell and SSH in one place, and the vast majority of them had not. However, all of them immediately got excited with the possibilities this opens, so I encourage you to try it out for yourselves and have fun

Dmitry Kniazev is a technical product marketing coordinator for Veeam and is based in Saint Petersburg, Russia. Dmitry specializes in virtualization, Linux and data Availability. Read Dmitry's blog at VMDK.CO.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like