Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying PowerShell Core for Linux Administrators Cookbook
  • Table Of Contents Toc
PowerShell Core for Linux Administrators Cookbook

PowerShell Core for Linux Administrators Cookbook

By : Prashanth Jayaram , Ram Iyer
close
close
PowerShell Core for Linux Administrators Cookbook

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)
close
close

Understanding objects

PowerShell is object-oriented, text being an object too. An object in PowerShell contains entities within it, which are called members. These members can be referred to using what is called the member access operator, which is nothing but a dot (.). In this recipe, we will look at picking members from within objects.

Since we are going to be dealing with members of objects, we will now look at one of the most important cmdlets in PowerShell: Get-Member.

How to do it...

  1. We will start by gathering the properties of our home directory. The path to this is stored in the automatic variable known as $HOME:
PS> Get-Item $HOME | Get-Member

Note the TypeName of the object—the very first line—as well as the MemberType column in the table.

  1. Next, check when the home directory was last written to:
PS> (Get-Item $HOME).LastWriteTime
  1. Can we find out when the parent of your $HOME was created?
PS> (Get-Item $HOME).Parent | Get-Member
PS> (Get-Item $HOME).Parent.CreationTime

To reiterate, PowerShell is not case-sensitive. However, to improve readability, we have stuck to the PowerShell convention of using camel case.
  1. What are the members of this object?
PS> (Get-Item $HOME).Parent.CreationTime | Get-Member
  1. Pick out the year:
PS> (Get-Item $HOME).Parent.CreationTime.Year

  1. Next, let's use a method from within the returned object and convert it into plain text. We saw that CreationTime is a DateTime object. Let's convert that into a plain string:
PS> (Get-Item $HOME).Parent.CreationTime.ToString()
  1. Find the type of object returned by the previous command. Press the up arrow key to bring back the previous command and pipe its output to Get-Member. Compare the output of Get-Member without .ToString():
PS> (Get-Item $HOME).Parent.CreationTime.ToString() | Get-Member
PS> (Get-Item $HOME).Parent.CreationTime | Get-Member

How it works...

This recipe is only here to show you that PowerShell outputs objects, and what an object looks like. We will look at leveraging the TypeName much later, when we modify the returned objects.

For now, we will stick to properties as the first step for understanding the object model of PowerShell. To demonstrate that methods are also available for use, we will use one that's among the simplest of them, the ToString() method; it's simple because it does not need any arguments—only empty parentheses.

The Get-Member cmdlet is very important in PowerShell because it lets you peek into the object that a certain cmdlet returned. It shows the type of the object returned (TypeName), and shows the properties and methods available. The properties and methods are of different data types themselves, which can be seen in the Definition column. As you get comfortable with objects, you will start to leverage these data types to simplify your tasks.

There's more...

Just because you can (and not because this is the way to do so), create a subdirectory within your home directory using the CreateSubdirectory() method from the object returned by Get-Item:

PS> (Get-Item $HOME).CreateSubdirectory('test-directory')

This should show you the new directory that you just created:

PS> Get-ChildItem $HOME # To list the contents of your home directory

See also

  • The Selecting columns from the output recipe from Chapter 4, Passing Data Through the Pipeline
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
PowerShell Core for Linux Administrators Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon