Sergey Nivens - Fotolia

Tip

Get started with PowerShell Core on Linux

PowerShell Core lets you configure and manage Windows, Linux and Mac-based servers in the data center. Learn how to install and navigate the command-line shell in this tip.

PowerShell is a Windows-only scripting language for managing systems such as Active Directory, Exchange and SharePoint. In 2016, Microsoft decided to make an open source version of PowerShell, named PowerShell Core.

PowerShell Core on Linux is a command-line tool that can help you construct binaries, object piping and desired state configuration for reliable configuration management, but the real benefit is its simplicity and readability. PowerShell Core behaves differently from a test-based shell, such as Bash, so you should learn how to use it, even if you're familiar with Bash.

PowerShell Core opens up data center standardization and automation possibilities with its compatibility for Windows, Linux and Mac machines, especially if you want to regularly implement management scripts. It's also helpful if you need a .NET object-oriented framework.

With PowerShell Core, you can manage parts of the data center beyond the server. VMware, NetApp and Cisco provide modules for PowerShell Core, which make PowerShell Core the glue that interacts with all of these different programs to automate virtualization and network activity in the data center.

You can use PowerShell Core to create a VM in VMware vSphere, back up Cisco network devices or develop network shares with NetApp Fabric-Attached Storage. Now that PowerShell Core is available, you can learn the Linux-equivalent cmdlets in PowerShell Core.

Installing PowerShell Core on Linux

Microsoft outlines methods to install PowerShell Core on various Linux distributions. This example uses CentOS, but you can also use Red Hat Enterprise Linux and Ubuntu.

First, check and see if you need to register Microsoft's repository:

[root@linuxmachine ~]$ curl https://packages.microsoft.com/config/rhel/7/prod.repo | sudo tee /etc/yum.repos.d/microsoft.repo

Next, use yum to install PowerShell Core:

[root@linuxmachine ~]$ sudo yum install -y PowerShell Core

Now, you can run pwsh and access the PowerShell Core command-line shell:

[root@linuxmachine ~]$ pwsh
PowerShell Core 6.1.1
Copyright (c) Microsoft Corporation. All rights reserved.
 
https://aka.ms/pscore6-docs
Type 'help' to get help.
 
PS /home/vagrant>

Viewing processes within PowerShell Core

The top command is a common way to view processes on Linux. In PowerShell Core, you use the Get-Process cmdlet, which, by default, shows the name, ID, nonpaged memory (NPM), paged memory (PM), working set (WS) and session ID (SI).

Here is the firewalld process in PowerShell Core:

PS /home/vagrant> Get-Process -Name firewalld
 
 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00      23.07       0.48   11231 231 firewalld

To stop processes, PowerShell Core uses the Stop-Process command. If you want to automate process termination, you can pipe the output of Get-Process to Stop-Process with the VBoxService on a Vagrant VM:

PS /home/vagrant> Get-Process -Name VBoxService | Stop-Process -WhatIf
What if: Performing the operation "Stop-Process" on target "VBoxService (754)".

Note that this example uses the –WhatIf parameter in Stop-Process. This suffix tests a cmdlet and shows what hypothetically happens if you run the cmdlet but it does not start the cmdlet.

Working with files

To work with a particular file, the PowerShell Core cmdlet most similar to the Linux ls command is Get-ChildItem, which retrieves items in a folder.

PS /home/vagrant/testfolder> Get-ChildItem
   
Directory: /home/vagrant/testfolder

 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          1/13/19   5:10 PM              0 1.txt
------          1/13/19   5:10 PM              0 2.txt
------          1/13/19   5:10 PM              0 3.txt
------          1/13/19   5:10 PM              0 4.txt
------          1/13/19   5:10 PM              0 5.txt

To filter files in a directory, –Filter is the most common cmdlet. For instance, this shows any file that has .txt as an extension:

PS /home/vagrant/testfolder> Get-ChildItem -Filter *.txt
 
    Directory: /home/vagrant/testfolder
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          1/13/19   5:10 PM              0 1.txt
------          1/13/19   5:10 PM              0 2.txt
------          1/13/19   5:10 PM              0 3.txt
------          1/13/19   5:10 PM              0 4.txt
------          1/13/19   5:10 PM              0 5.txt

To view and add content to a file, use the Get-Content and Set-Content commands. Get-Content is similar to cat on Linux.

PS /home/vagrant/testfolder> Get-Content ./testfile.txt
This is a test string.

To change any part of the file, you can use a replace operator from Get-Content and pipe that to Set-Content. This example changes string to thing in the testfile.txt file:

PS /home/vagrant/testfolder> (Get-Content ./testfile.txt).Replace('string','thing') | Set-Content ./testfile.txt 
PS /home/vagrant/testfolder> Get-Content ./testfile.txt                                                         
This is a test thing.

Testing network ports


Overview of PowerShell Core 6.0 on Linux

In Linux, the nc (netcat) command troubleshoots remote network ports to different hosts. This is done in PowerShell Core on Linux with the Test-Connection cmdlet. Once you run Test-Connection, you'll get a Boolean data type that states if the connection is true or false.

The following connection tests google.com to see if port 80 is open, which it is:

PS /root> Test-Connection -TargetName google.com -TCPPort 80
True

Test-Connection can also just ping for network connection status:

PS /root> Test-Connection -TargetName google.com
Pinging google.com [172.217.11.46] with 32 bytes of data:
Reply from 172.217.11.46: bytes=32 time=5ms TTL=128
Reply from 172.217.11.46: bytes=32 time=5ms TTL=128
Reply from 172.217.11.46: bytes=32 time=5ms TTL=128
Reply from 172.217.11.46: bytes=32 time=5ms TTL=128
Ping complete.

To test multiple hosts, you can put all hosts in an array within Test-Connection, but it does not show output of specific hosts:

PS /root> $Hosts = 'google.com','espn.com','cnn.com','blank.org'
PS /root> Test-Connection -TargetName $hosts -TCPPort 80
True
True
True
True

To show the host in the output, you can use a ForEach loop in PowerShell Core and pipe the $hosts variable into it with Test-Connection:

PS /root> $Hosts | ForEach-Object {$Output = Test-Connection -TargetName $_ -TCPPort 80;Write-Output "$_,$Output"}
google.com,True
espn.com,True
cnn.com,True
blank.org,True

Writing a script

PowerShell Core scripts use the file extension .ps1 and are called within or outside of the PowerShell Core command-line shell. You can manually run scripts as needed or automate them to perform daily tasks.

Inside this test.ps1 script, use the Get-ChildItem cmdlet, which means, when this script runs, it returns the files and directories within the path you are in.

[root@linuxmachine ~]# cat test.ps1
Get-ChildItem
[root@linuxmachine ~]# pwsh -file test.ps1
 
    Directory: /root
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          6/26/17   8:57 AM           1318 anaconda-ks.cfg
------           8/3/17   9:25 AM            593 Get-Stuff.ps1
------          1/25/18   3:24 PM           1343 TestScript.ps1
------          1/14/19  10:18 AM             14 AnotherScript.ps1
------          7/10/17   4:40 PM              0 file.txt

To call a script outside of the PowerShell Core command-line shell, use the pwsh command with the –File parameter.

Dig Deeper on Data center ops, monitoring and management

SearchWindowsServer
Cloud Computing
Storage
Sustainability
and ESG
Close