Introduction to PowerShell Core on Linux for the SQL Server DBA Part 1
We are starting to deploy SQL Server on Linux for our new applications. I have only been a Windows Server administrator and have not had any opportunity to work with Linux, but I am familiar with PowerShell for managing Windows and SQL Server. Can I work with PowerShell on Linux?
Windows PowerShell is an extensible command-line shell and associated scripting language built on top of the .NET Framework. Released in 2006, it was originally designed to run on the Windows operating system. In 2016, Microsoft open-sourced PowerShell and called it PowerShell Core - a fork of the Windows PowerShell codebase - allowing it to run on multiple non-Windows platforms such as Linux and macOS. This makes it easy to standardize and automate administration of the Windows and Linux operating systems, including the applications that run on them.
In the past, Windows administrators need to learn bash commands and bash scripting to manage Linux. PowerShell Core leverages Windows administrators' existing knowledge on Windows PowerShell to manage Linux. And since SQL Server now also runs on Linux, it allows DBAs to manage SQL Server on Linux using the tools that they are already familiar with - SQL Server PowerShell modules and dbatools.
In this series of tips, we will look at leveraging PowerShell Core on Linux together with the SQL Server PowerShell modules and dbatools to manage SQL Server. Let's start by installing PowerShell Core on Linux.
Install PowerShell Core on Linux
PowerShell Core is supported to run on the following Linux distributions.
- Ubuntu 16.04
- Ubuntu 18.04
- Ubuntu 18.10
- Ubuntu 19.04
- Debian 8
- Debian 9
- Debian 10
- CentOS 7
- Red Hat Enterprise Linux (RHEL) 7
- openSUSE 42.3
- openSUSE Leap 15
- Fedora 27
- Fedora 28
- Arch Linux
This tip will only focus on the Linux distributions that SQL Server is supported on, mainly RHEL/CentOS 7 and Ubuntu 16.04. However, you can install PowerShell Core on a Linux distribution that doesn't support SQL Server and use that to manage SQL Server on Linux remotely via PowerShell Remoting and SSH.
Install PowerShell Core on RHEL/CentOS
To install PowerShell Core on RHEL/CentOS,
- Run the command below to download the Microsoft SQL Server Red Hat repository configuration file.
sudo curl -o /etc/yum.repos.d/microsoft.repo https://packages.microsoft.com/config/rhel/7/prod.repo
- Run the command below to install PowerShell Core on RHEL/CentOS. This will download the PowerShell Core installation packages for RHEL/CentOS. Once the download completes, it will automatically run the installation.
sudo yum install -y powershell
Install PowerShell Core on Ubuntu 16.04
Installation on Ubuntu is a bit different because, unlike in RHEL/CentOS where they are implicitly included as part of the installation process, you have to explicitly define the Microsoft GPG key. To install PowerShell Core on Ubuntu 16.04,
- Run the command below to download the Debian Software Package file containing the Microsoft repository file and the GPG key.
sudo wget https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
- Run the command below to register the downloaded Microsoft repository file and GPG key on the Ubuntu Linux system.
sudo dpkg -i packages-microsoft-prod.deb
- Because you have made modifications to your Ubuntu system by adding the Microsoft Ubuntu package repository file, you need to run the apt-get update command prior to installing PowerShell Core
sudo apt-get update
- Run the command below to install PowerShell Core on Ubuntu 16.04. This will download the PowerShell Core installation packages for Ubuntu. Once the download completes, it will automatically run the installation.
sudo apt-get install -y powershell
Switching to the PowerShell Core Shell
Once the installation completes, you can run the command below to start PowerShell Core.
You will see your terminal session prompt change, telling you that you are now in a PowerShell Core session.
Bash commands are still available in the PowerShell Core shell so you can take advantage of both PowerShell Core cmdlets and standard Linux commands. For example, I often use the cat command on Linux to read contents of files. The command below uses the cat command on a CentOS Linux system to read the configuration of the network adapter eth0. I also use the pipe character to pipe the output of the cat command to the Select-String cmdlet.
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | Select-String DEVICE
Exploring the PowerShell Core Shell Environment
Since this is no longer a Windows operating system, some of the Windows PowerShell cmdlets are not available. Run the command below to display the different PowerShell Core cmdlets available on Linux.
I always start with these three cmdlets – Get-Command, Get-Help, and Get-Member – when working on a new system to familiarize myself with the available PowerShell cmdlets. It helps when other third-party PowerShell modules are available so I know what I can work with. For example, I can use the Get-Command cmdlet to display the usage of a specific cmdlet. The command below displays the syntax for using the Get-Process cmdlet. It's the same command for both Windows and Linux.
Get-Command Get-Process -Syntax
You can also list the available aliases that map to the PowerShell Core cmdlets by running the Get-Alias cmdlet. Listing the different aliases can give you some hints as to whether a command is a native Linux command or a PowerShell Core alias. For example, you might use the cls command on Linux because it's what you've gotten used to on Windows to clear the terminal when you should be using the clear command (I make that mistake a lot when switching between Windows and Linux command-line). The cls command is defined as an alias for the Clear-History PowerShell Core cmdlet.
Objects versus String
One of the challenges that you'll face when working on Linux is that the output of commands is in text. This requires a lot of string manipulation especially if you need to retrieve specific string values. In comparison, the output of PowerShell commands are objects. You will see this when you compare the ps command versus the Get-Process cmdlet.
This is not a big deal if you are just working with PowerShell cmdlets. However, since there aren't as much cmdlets on PowerShell Core as there are with Windows PowerShell, you still need to learn basic Linux commands to properly administer the system. Let's say you want to retrieve just the PID of the process running PowerShell Core on your Linux system, running the ps command would look something like this.
ps ax | grep pwsh | cut -f2 -d" "
It pipes the output of the ps command to the grep command to display the pwsh process. The cut command is used for string manipulation, to "cut" out the sections from each line of output and writes the result to the terminal. Whereas, the equivalent PowerShell command would look something like this.
Get-Process -Name pwsh | Select-Object Id
In order to work with PowerShell objects the way you would in Windows PowerShell when combining Linux commands with PowerShell cmdlets is to create a custom object based on the output of a Linux command. We will look at how to combine Linux commands with PowerShell cmdlets to manage SQL Server in this series of tips. In the next tip in this series, you will explore working with SQL Server PowerShell cmdlets and dbatools for managing SQL Server on Linux.
- Read the Microsoft documentation on installing PowerShell Core on Linux
- Review the previous tips on Windows PowerShell for the SQL Server DBA to compare the difference between Windows PowerShell and PowerShell Core on Linux
About the author
View all my tips
Article Last Updated: 2020-03-11