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)

Dissecting a .NET Core object

This is a tangential recipe, in case you are interested in seeing how PowerShell has been implemented.

.NET Core works on a cross-platform standard Common Language Infrastructure. Therefore, it has been possible to encapsulate the internal workings of Linux using .NET Core. As we will see in future chapters, PowerShell is object-oriented, just like .NET Core. For this demonstration, we will pick a simple system class called System.IO.DirectoryInfo to show information about a certain directory. We will also compare the output of the .NET Core object to the output of a PowerShell cmdlet, which also shows information about a certain directory.

You do not have to remember the names of the .NET Core classes or methods, or their syntax to work with PowerShell; that is the whole point of the existence of PowerShell. However, if you need information on the .NET libraries and the classes, extensive help documentation is available online ().

Getting ready

Every object has members—properties and methods. In case you're new to the concepts of object-oriented programming, properties are qualities of an object (what the object has), and methods are the capabilities of an object (what the object can do). Therefore, to quote (arguably) the most overused example of properties and methods: if a car is an object, its height, its color, and so on would be its properties, while accelerating, braking, and so on would be the methods that the object supports.

How to do it...

  1. With PowerShell, .NET Core is also installed as a dependency. Let's create an object in PowerShell that will call a .NET class and its default constructor. This constructor requires an argument:
PS> New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $HOME

Here, /home/ram is my home directory. Replace this path with yours.

  1. Use the Get-Item cmdlet with the same argument as before, and see what you get:
PS> Get-Item $HOME
  1. Close! Now, let's look at the members of the output object we just received by using Get-Member:
PS> Get-Item $HOME | Get-Member

This will list a series of members (properties, methods) that are part of the output. We're primarily concerned about the very first line for now.

How it works...

Note the very first line of the output, TypeName: System.IO.DirectoryInfo. That is the exact type name we used when we created the .NET object:

This proves that the same task of showing information on the current working directory can be achieved by either calling a .NET constructor or by running a PowerShell cmdlet, the implication being that PowerShell cmdlets are simply encapsulated .NET code. In essence, Get-Item calls the System.IO.DirectoryInfo class under the hood, with the arguments passed along with the cmdlet.

Like they say: "If the C# guys can do it, so can you."

See also