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)

Installing PowerShell

In this book, we will focus on installing PowerShell on Ubuntu (16.04 and 18.04) and CentOS 7, since Ubuntu and CentOS were among the first to be supported by the PowerShell project.

PowerShell is available in two releases: stable and preview. The stable releases are suitable for a production environment since they are more reliable. The preview releases are for test environments where the administrators are allowed to feel a little adventurous. The expectation from the administrators is to report bugs they come across, along with providing feedback on the capabilities of it.

As of the Preview 4 release of PowerShell 6.1, pwsh (stable) and pwsh-preview (preview) can be installed side by side, without the worry of interference.

Getting ready

Linux administrators will already know how to do this. If you are new to Linux, getting ready to install PowerShell on your computer is simple. You simply need a working Linux computer that you have administrator privileges on. Depending on what mode you pick to install PowerShell, you may or may not need a package manager. Chances are, your Linux distribution already has a package manager available.

Also, many of the future recipes will require a desktop environment so that you can work with tools such as Visual Studio Code.

Ubuntu does not ship with curl. Install curl by running the following command:

$ sudo apt install curl

How to do it...

As we have already discussed, we will look at the procedure of installing PowerShell on Ubuntu (and its derivatives) as well as CentOS (and its derivatives).

Installation on Ubuntu

There really are many ways to install PowerShell on your computer. Since we are installing PowerShell on Ubuntu, we will look at two ways of doing so. The first is by adding Microsoft's key, registering the repository, and then using the Advanced Package Tool (APT) to install PowerShell. The second method is by doing so directly, using the .deb package from GitHub.

Installing from the repository

Ubuntu 16.04 and Ubuntu 18.04 have the stable release of PowerShell available. Follow these steps if you wish to install PowerShell on Ubuntu:

  1. Download the GPG keys for the Microsoft repository:
$ # On Ubuntu 16.04
$ wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb

$ # On Ubuntu 18.04
$ wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
  1. Register the GPG keys:
$ sudo dpkg -i packages-microsoft-prod.deb
  1. Update the package list using the following command:
$ sudo apt update
  1. Install PowerShell using apt-get:
$ # To install PowerShell (stable)
$ sudo apt install -y powershell

$ # To install PowerShell (preview)
$ sudo apt install -y powershell-preview

  1. Run PowerShell using the following command:
$ # If you installed PowerShell (stable)
$ pwsh

$ # If you installed PowerShell (preview)
$ pwsh-preview

Installing via direct download

Follow these steps to install PowerShell on Ubuntu:

  1. Go to https://github.com/powershell/powershell.
  2. Scroll down to the table that contains the list of Linux distributions officially supported by the PowerShell team.
  3. Click on the relevant link, that is, .deb under Downloads (stable) or Downloads (preview).
  4. Read the installation instructions if you need additional information.
  5. Use dpkg to install the package:
$ sudo dpkg -i powershell-version-os-osversion_architecture.deb
$ sudo apt install -f

Installation on CentOS

The installation of PowerShell on CentOS 7 (or Red Hat 7) also has two methods: the repository and a direct download. The process is similar to that of installation on Ubuntu Linux. While it is recommended that PowerShell is installed from the repository, use the second method—the direct download method—should you choose otherwise.

Installing from the repository

To begin the installation, follow these steps:

  1. First, register the Microsoft repository:
$ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo
  1. Next, install PowerShell using yum:
$ sudo yum install -y powershell

Installing via direct download

Follow these steps to install PowerShell on CentOS:

  1. Go to https://github.com/powershell/powershell.
  2. Scroll down to the table that contains the list of Linux distributions officially supported by the PowerShell team.
  3. Click on the relevant link, that is, .rpm under Downloads (stable)—we will stick to the stable release.
  4. Read the installation instructions if you need additional information.
  5. Install the RPM package using the following command (assuming your download is at ~/Downloads and your pwd is ~/Downloads):
$ sudo yum install <the-downloaded-file.rpm>

Using the Snapcraft package

If your distribution supports the installation of Snapcraft (snap) packages, you can install PowerShell using the snap command:

  1. Install snapd if you do not already have it. Here is how you do this on Ubuntu:
$ sudo apt install snapd
  1. Next, install the PowerShell Snapcraft package:
$ snap install powershell --classic
  1. You may be prompted for your credentials. Enter your credentials at the prompt.
  2. Launch PowerShell:
$ pwsh

Using the binary archives

If your package manager does not have PowerShell, use the binary archives:

  1. Install the dependencies based on your operating system. Here are the dependencies required in the case of Ubuntu 16.04, Ubuntu 18.04, and CentOS:
Operating System Dependencies
Ubuntu 16.04 libc6, libgcc1, libgssapi-krb5-2, liblttng-ust0, libstdc++6, libcurl3, libunwind8, libuuid1, zlib1g, libssl1.0.0, libicu52
Ubuntu 18.04 libc6, libgcc1, libgssapi-krb5-2, liblttng-ust0, libstdc++6, libcurl3, libunwind8, libuuid1, zlib1g, libssl1.0.0, libicu60
CentOS7 libunwind, libcurl, openssl-libs, libicu
  1. Use curl to get the archived file:
$ curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v6.1.0/powershell-6.1.0-linux-x64.tar.gz
  1. Create the directory where PowerShell will be installed:
$ sudo mkdir -p /opt/microsoft/powershell/6.1.0
  1. Unpack the binaries into the directory you just created:
$ sudo tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/6.1.0
  1. Add execute permissions to the file:
$ sudo chmod +x /opt/microsoft/powershell/6.1.0/pwsh
  1. Create a symbolic link to point to pwsh:
$ sudo ln -s /opt/microsoft/powershell/6.1.0/pwsh /usr/bin/pwsh

Bonus– using the Install-PowerShell script (including on macOS)

The official PowerShell repository has a script that detects your operating system and installs PowerShell based on the operating system and the distribution.

Your computer must have the following:

  • The Bash shell
  • sed
  • A native package manager

We need the Visual Studio Code IDE so that we can follow the exercises in this book. Run the following command to commence the installation:

$ bash <(wget -O - https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh) -includeide
If you do not have a desktop environment on your computer, you will not be able to install Visual Studio Code. Either install a desktop environment or omit the -includeide switch.

For more information, go through the README file at https://github.com/PowerShell/PowerShell/blob/master/tools/install-powershell-readme.md.

How it works...

The package manager installs the package with all of its dependencies and makes the relevant commands available for use.

Starting with Snap 2.20, you can install Snapcraft packages with relaxed security boundaries using the --classic switch. Snaps usually run in a sandboxed environment, meaning that they are separated from the host, so to speak. However, PowerShell, being a shell, needs access to all of the host resources so that it can function as desired, hence the --classic install. Also note that this is not supported on systems such as Ubuntu Core.

See also

  • The Comparing Windows PowerShell and PowerShell Core recipe