Book Image

PowerShell Core for Linux Administrators Cookbook

By : Prashanth Jayaram, Ram Iyer
Book Image

PowerShell Core for Linux Administrators Cookbook

By: Prashanth Jayaram, Ram Iyer

Overview of this book

PowerShell Core, the open source, cross-platform that is based on the open source, cross-platform .NET Core, is not a shell that came out by accident; it was intentionally created to be versatile and easy to learn at the same time. PowerShell Core enables automation on systems ranging from the Raspberry Pi to the cloud. PowerShell Core for Linux Administrators Cookbook uses simple, real-world examples that teach you how to use PowerShell to effectively administer your environment. As you make your way through the book, you will cover interesting recipes on how PowerShell Core can be used to quickly automate complex, repetitive, and time-consuming tasks. In the concluding chapters, you will learn how to develop scripts to automate tasks that involve systems and enterprise management. By the end of this book, you will have learned about the automation capabilities of PowerShell Core, including remote management using OpenSSH, cross-platform enterprise management, working with Docker containers, and managing SQL databases.
Table of Contents (19 chapters)

Comparing the outputs of Bash and PowerShell

PowerShell and Bash are both shells, and are capable of interacting with the kernel. Just like Bash can run on Windows, PowerShell can now run on Linux. While almost all of the aspects of which shell is better than the other is debatable, and the choice of shell is simply a matter of personal preference today, it is true that PowerShell is as powerful as .NET Core can get.

The primary difference between the two shells is that PowerShell outputs objects, while Bash returns text. Manipulation of the output in Bash involves manipulating text first, and then running further commands on the manipulated text to fetch the desired output. PowerShell, on the other hand, handles content as objects and, by design, requires comparatively less manipulation.

Structured data, as noted by Jeffrey Snover (the inventor of Windows PowerShell), is getting more popular as days pass, and structured data is where PowerShell shines the most.

Getting ready

In this recipe, we are going to pick one example to show you how simple and efficient it is to handle file metadata using PowerShell, primarily since the output is an object. We will list the files and folders within our home directory, along with the date and time of modification using both ls in Bash and Get-ChildItem in PowerShell.

We will use PowerShell to run the Linux commands as well.

How to do it...

  1. At the Bash prompt, enter ls -l to list all of the files, along with the metadata that the command shows by default:
  1. Go to the Terminal that has PowerShell running and type in Get-ChildItem at the prompt:

  1. Now, let's pick only the name of the folders and the last modified date and time. This is done in Bash by passing the output of ls -l to awk:
ls -l | awk '{print $6, $7, $8, $9}'
  1. Next, let's pick the same information on PowerShell as well:
Get-ChildItem | select LastWriteTime, Name

As you may have noticed, the output is very similar in both cases. However, with PowerShell, you can see the names of the columns as well, which means that you do not have to look for further documentation. Also, the selection of columns is simpler in PowerShell; no text manipulation is required. On the other hand, in Bash, we can use the awk command to manipulate the text output.

  1. Let's go one step further and create a subdirectory with a space in the name:
$ mkdir 'test subdirectory'
$ ls -l | awk '{print $6, $7, $8, $9}'

Note that what should have been test subdirectory appears as test. Compare this with the output from PowerShell:

How it works...

PowerShell reads content from the filesystem as objects, not as text. Therefore, you perform a selection of the desired columns (or, as we shall later see, properties) directly. Bash, on the other hand, outputs text, columns from which are manipulated using a delimiter.

To demonstrate that this is the case, we created a new subdirectory with a space in its name, and we performed the column selection just like we did before, only in this case we did not get the complete name of the new subdirectory. This is because the name contained a whitespace, which is a delimiter in awk.

Comparing Bash and PowerShell is like comparing apples and oranges—in more ways than one. However, understanding the differences helps us leverage each of the tools to our benefit.

See also

  • The Selecting columns from the output recipe from Chapter 4, Passing Data Through the Pipeline