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)

Finding and installing PowerShell modules

Loosely coupling the components is one of the keys to the success of a framework. PowerShell follows this principle as well. All cmdlets are packaged within PowerShell modules. The modules can be first-party provided, created by you, or created by other software vendors.

Initially, extending PowerShell capabilities was done using snap-ins. With PowerShell 2.0, PowerShell modules were introduced. One other place that Microsoft has gotten it right is advocating the use of modules over snap-ins. The installation of PowerShell modules, which could have been a hassle in the past, has been streamlined today. PowerShell now comes pre-packaged with a package manager called PowerShellGet, which connects to the official Microsoft PowerShell Gallery (https://www.powershellgallery.com). The PowerShell Gallery is an online repository that contains modules, scripts, and other utilities that have been created by providersas well as the communitythat administrators can download and install to extend PowerShell's capabilities.

While it is possible to download PowerShell modules from third-party sites, our focus in this book will be on the PowerShell Repository.

How to do it...

We will start by finding cmdlets that work with modules, and proceed to work with these modules:

  1. Launch PowerShell by running pwsh on the Terminal.
  2. Look for commands that work with modules:
PS> get-command -no module PS> # Verbose version: Get-Command -Noun 'Module'

The output should look something like this:

Pick Find-Module for this task. If you would like to, use the Get-Help cmdlet to learn about what Find-Module does.

  1. Search for a module to help you work with Docker containers:
  1. Now, install the module. You will need to use superuser privileges to install the Docker module. Use the following command to install Docker without having to exit the current PowerShell session:
PS> sudo pwsh -c install-module docker

If you would rather use a new session and call the Install-Module cmdlet from within it, terminate the current PowerShell session and launch a new one with sudo:

  1. To update a module, use the Update-Module cmdlet:
PS> Update-Module docker
  1. To uninstall a module from the machine, use the Uninstall-Module cmdlet:
PS> Uninstall-Module docker

  1. To load a certain module into the current session, use the Import-Module cmdlet. You do not need elevated privileges to load or unload a module to or from the session:
PS> Import-Module docker
  1. To remove a module from the session, use the Remove-Module cmdlet:
PS> Remove-Module docker

How it works...

Hundreds of modules, scripts, and Desired State Configuration resources have been registered with the PSGallery repository. Microsoft now recommends using this repository for module management. Using the Find-Module cmdlet, PowerShell makes a connection to the repository and gets a list of all available modules. Then, it runs a search on the returned results based on the criteria you mention.

In PowerShell, Find usually means working with the internet, and Get is usually used to get objects from the local machine.

You can also find scripts that can perform repetitive tasks. To find scripts, use the Find-Script cmdlet. It works similar to the Find-Module cmdlet, except that it finds individual scripts rather than modules.

Installation of modules is a simple process of pulling the modules down from the repository, similar to npm or chocolatey. These tasks are taken care of by the PowerShellGet module, which ships with PowerShell. Installing, updating, and uninstalling PowerShell modules may require elevated privileges.

To load the capabilities of a module into the PowerShell session, use Import-Module. PowerShell modules have members such as cmdlets, functions, aliases, and so on for sessions. Import-Module loads these members into the session. By default, the sessions are unloaded when exiting the session. However, if you would like to manually unload them, use the Remove-Module cmdlet. Remove-Module does not remove the entire module; it just unloads it.

The modules are installed in one of the $env:PsModulePath directories. You don't need to specify the path to modules that are installed this way. If the module files are saved elsewhere, you will need to specify the path to the module's psd1 or psm1 file when calling Import-Module. Also, some modules enable automated loading. Just call a cmdlet from the module, and the module will auto-import.

Import-Module has parameters such as -Prefix and -NoClobber. The -Prefix parameter adds the specified prefix to the nouns of all of the cmdlets in the module. For instance, Import-Module docker -pre dm will import the Docker cmdlets as Invoke-dmDockerCommand instead of Invoke-DockerCommand. The -NoClobber switch helps in cases where there may be cmdlet name conflicts; it prevents the already loaded cmdlets from being replaced by the ones from the module being imported.

There's more...

Try installing the module in portable mode, by first saving the module using Save-Module and then calling Import-Module with the path to the module file as the parameter.

You can also get the list of directories from which PowerShell discovers modules. At the prompt, type the following and press the Enter key:

PS> $env:PSModulePath

See also

  • The Writing a script module recipe in Chapter 12, Advanced Concepts of Functions
  • The Calling a PowerShell script recipe in Chapter 2, Preparing for Administration using PowerShell