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)

Working with aliases

An alias, as the meaning goes, is an alternative name for cmdlet. They serve two purposes:

  • To reduce the number of keystrokes
  • To make the transition to PowerShell smoother

Traditionally, aliases were created in PowerShell so that Windows and Linux administrators did not find the new framework intimidating to work with. Regardless, aliases are best used only on the command line, and not in scripts, because some aliases are not aliases in Linux, and, in general, aliases affect readability. (For instance, it would take conscious effort to realize that gbp stands for Get-PSBreakPoint.)

How to do it...

As we have already mentioned, it is simple to think in PowerShell. When we know that the verb to fetch any information locally is Get, and the noun in this case would be Alias, the cmdlet should be Get-Alias :

  1. Run Get-Help to understand how to use the cmdlet:
PS> Get-Help Get-Alias
If you're unsure about any command, or you would like to reduce keystrokes without involving aliases, use tab-completion. Write a part of the cmdlet or parameter and press the Tab key. PowerShell will complete the command for you, or show you suggestions, based on which platform you're doing this on.

  1. According to the help documentation, all of the parameters for Get-Alias are optional (they are all enclosed in []). Therefore, simply running Get-Alias will give us a list of all of the aliases that are available in the current instance of PowerShell:
  1. Now, let's try to resolve the gbp alias to the PowerShell cmdlet that it actually runs in:
PS> Get-Alias gbp
  1. Now, do the opposite: get the alias for a certain cmdlet. The Definition in the second parameter set of the help documentation should be used. The output shows the actual PowerShell cmdlet that runs when an alias is called:
PS /home/ram> Get-Alias -Definition Get-ChildItem

  1. We can see two aliases as output, both of which run Get-ChildItem under the hood. Now, let's run dir as well as Get-ChildItem and compare their outputs:
PS> dir
PS> Get-ChildItem
  1. The two outputs are identical. Now, let's look at what type of object the commands return:
PS> /home/ram> dir | Get-Member
PS> /home/ram> Get-ChildItem | Get-Member

They returned the same object as well.

Now, let's create an alias:

  1. First, identify a word that you'd like to use as the alias. For example, let's consider listdir.
  2. Run listdir on PowerShell to ensure that such a cmdlet (or a Linux command) does not already exist.
  3. List out the cmdlets that deal with aliases by running the following command:
PS> Get-Command *alias
  1. New-Alias is the cmdlet we are looking for, since it creates a new alias.
In PowerShell, the Set verb is used to modify something. Therefore, Set-Alias is used to modify an alias that already exists.
  1. Read the help documentation for New-Alias by running the following command:
PS> Get-Help New-Alias

The help document indicates that only the Name and the Value parameters are mandatory. We will only use these two to create this simple alias.

  1. Run the following command to create the custom alias:
PS> New-Alias listdir Get-ChildItem

  1. Check whether the alias was created as desired or not:
PS> Get-Alias listdir
  1. Next, run the alias to see what output it gives:
PS /home/ram> listdir

That is the output that we are familiar withthe output of Get-ChildItem.

Aliases are ephemeral by default. They exist only as long as your PowerShell session exists. To use custom aliases without having to recreate them each time, export these aliases (the instructions for which are in the next recipe) and import them using your PowerShell profile. We will look at profiles in Chapter 2, Preparing for Administration using PowerShell.

Since aliases are ephemeral, let's export the alias we created. The output of Get-Command a few steps ago showed Export-Alias. Get-Help for the cmdlet shows that there are two ways to export the aliases: as a comma-separated values file, and as a script.

Export the aliases as a CSV file:

PS> Export-Alias aliases.csv

Export the aliases as a script as well:

PS> Export-Alias aliases.ps1 -As Script

View the contents of both files either using cat or Get-Content.

Optionally, edit the file to remove all of the aliases except the ones you created. The custom aliases can be found at the bottom of the list.

Next, import the aliases into your PowerShell session:

  1. Restart PowerShell.
  2. Check if the listdir alias that you created exists:
PS> Get-Alias listdir
  1. Now, import either of the alias exports:
PS> Import-Alias ./aliases.csv
PS> # Or, simply call the script, ./aliases.ps1
If you did not remove the pre-existing aliases, you may receive several errors, each saying that the new alias could not be created. There are two ways to handle this: the first way is to remove the default aliases from the export file (which is recommended), and the second way is to use the -Force parameter (this may still result in errors, but there would be significantly fewer).

If you would like to add these aliases to your session, follow the Enabling automated execution of commands for each load recipe in Chapter 2, Preparing for Administration Using PowerShell.

How it works...

Aliases are nothing but mappings that are done within PowerShell. The short words are mapped to PowerShell cmdlets; the cmdlets are recorded as Definition in each of the aliases. Aliases also support the same parameters as the cmdlet as well, since aliases are merely pointers to the right cmdlet. When you run anything on PowerShell, PowerShell checks its list of cmdlets and aliases (among other definitions) to understand what you are asking for. When PowerShell encounters an alias, it looks for which cmdlet it points to, and runs the cmdlet.

Using New-Alias, you can create a pointer with a custom name that points to the desired PowerShell cmdlet. When the custom cmdlets are exported as a script, the contents show New-Alias for each of the aliases available in the session.

Exporting aliases as CSV makes it easier to extend within a text editor if needed. The Import-Alias cmdlet understands that the first column in the CSV is the name of the alias, and that the second is its definition.

There's more...

Add more content to your aliases, such as descriptions. Refer to the help documentation for Get-Alias to see what more you can do with aliases.

If you have a Windows computer, try running Get-Alias against Linux commands such as ls.

See also

  1. The Understanding cmdlets and parameters recipe from Chapter 2, Preparing for Administration Using PowerShell
  2. The Enabling automated execution of commands for each load recipe from Chapter 2, Preparing for Administration Using PowerShell