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)

Updating and using Help

Help documentation is very important in PowerShell, regardless of whether a certain cmdlet is packaged by providers such as Microsoft, VMware, or Citrix, or community-created. Help is not a switch in PowerShell. PowerShell uses the Get-Help cmdlet (pronounced command-let) to fetch the Help information enclosed within cmdlets.

By default, PowerShell is installed with minimal Help information, which contains only the description and the parameters. In this recipe, we will update Help and learn to get Help by using cmdlets, specific parameters, or certain keywords.

Getting ready

To go through this recipe, you must have PowerShell 6 installed on your computer, and you must have administrator privileges available. To install PowerShell, refer the Installing PowerShell recipe in this chapter.

How to do it...

To update the Help files for the locally installed PowerShell modules, follow these steps:

  1. Type exit to exit PowerShell. This is so that you can relaunch PowerShell with elevated privileges. This is required for some of the modules, based on the permissions used to install them.
  2. Enter sudo pwsh (or sudo pwsh-preview) to launch PowerShell as a superuser.
  3. At the PS> prompt, run Update-Help.
  1. Wait for the update progress bar to appear. The bar will fill as the Help files download onto your computer:
  1. Use exit to exit PowerShell as a superuser, and launch PowerShell as a regular user.

We can now proceed and fetch Help information for other cmdlets.

  1. At the command prompt, enter the following command:
PS> Get-Help Write-Host

You will get an output similar to the following:

  1. Next, gather different levels of Help information by using the following commands:
PS> Get-Help Write-Host -Full
PS> Get-Help Write-Host -Examples
PS> Get-Help Write-Host -Online
  1. Pass a different cmdlet as a parameter to Get-Help. Note the two groups of parameters:
PS> Get-Help Get-Command
  1. Compare the outputs of the different commands you ran.
  2. Now, look for Help information for the specific parameter of Write-Host (the second command in the following code is for reference; this states what the first command means to PowerShell):
PS> get-help write-host -par foregroundcolor
PS> # Verbose version: Get-Help -Name Write-Host -Parameter ForegroundColor

Now let's look for a certain keyword within the Help information:

  1. Convert the output of Get-Help Get-Command into text, one string at a time, rather than putting in the entirety of the Help information as a single string:
PS> get-help get-command | out-string -s
PS> # Verbose version: Get-Help Get-Command | Out-String -Stream
  1. Pipe this to the Select-String cmdlet to perform a grep-like operation:
PS> get-help get-command | out-string -s | select-string 'wildcard'
PS> # Verbose version: Get-Help -Name Get-Command | Out-String -Stream | Select-String -Pattern 'wildcard'
  1. This gives you an output of a line that contains wildcard:

How it works...

It is inconvenient to leave your Terminal to read Help online. We would prefer to have all of the Help available locally. PowerShell can potentially work on low-memory devices such as Raspberry, but saving all of the Help in the limited space we have is not advisable. This is one of the reasons PowerShell comes with minimal Help by default. The reason updating Help requires elevated privileges is that the Help information is stored within the shell. Therefore, non-administrators may not be able to update Help without administrator intervention.

As a general practice, cmdlets come pre-packaged with Help information, and Get-Help works like the man command in Linux. The output of Get-Help has the name of the cmdlet, the syntax to use with the cmdlet, the alias(es) available for the cmdlet and more online Help if required. -Full and -Examples are (mutually exclusive) switches that tell PowerShell about the level of Help you need.

The groups of parameters, as seen in the case of Get-Command, under SYNTAX, are called parameter sets. They tell us which parameters can be used together. Parameters not appearing in the same parameter set cannot be used together. For example, you cannot use -Noun and -Name with Get-Command at the same time; it wouldn't be logical to do so.

If the Help information seems elaborate, and you would like to learn about a specific parameter of a cmdlet, you can specify that parameter, and the Help will be filtered accordingly. Since the output of most PowerShell cmdlets is objects, it is easy to select the necessary object and discard the rest from the output. Also note that this is the recommended approach to fetch Help that's specific to a certain parameter.

PowerShell, in general, is case-insensitive. Therefore, Noun and noun, or Parameter and parameter, mean the same thing to it. The use of CamelCase is the general convention in PowerShell. With the almost ubiquitous tab completion, this is a cakewalk.

Next, let's talk about searching Help for specific keywords. PowerShell outputs objects. However, at the end, when the content comes to the host, the content can be processed by Linux commands such as grep. Therefore, to search Help for specific keywords, you could always pipe the output to grep. If you would like to go the PowerShell way (especially if you go by the write once, run everywhere philosophy), this requires minor modifications. First, we fetch the Help; the output is an object (so that it can be processed further if needed). We convert this into a string using Out-String, since search is nothing but string matching.

PowerShell cmdlets output single or multiple instances of objects (we will discuss objects in more detail in the Understanding objects recipe). Out-String waits for all of the Get-Help commands to be processed and converts the output into a single concatenated string. To break the string into chunks, we use the -Stream switch, which instructs Out-String to not concatenate the output. This way, each paragraph is processed separately. Next, we use the Select-String cmdlet, along with the search keyword to perform keyword matching. The output of this is the chunk (paragraph, in this case) with the keyword.

See also

  • The Working with aliases recipe
  • The Updating and using Help recipe
  • The Running cmdlets with minimal keystrokes recipe in Chapter 2, Preparing for Administration using PowerShell