Installing PowerShell Core on Amazon Linux
In preparation for my latest course in the AWS Networking Deep Dive series, I wanted to install PowerShell Core on an Amazon Linux instance to test out cross-platform compatibility for some scripts.
Specifically, I wanted to see if I could use methods in the System.Net.Dns class to perform name resolution. The dnsclient PowerShell module provides some cmdlets for this very purpose, but that module is Windows-only, and I needed something that would work on across different platforms.
To my surprise, it wasn’t as easy as just running sudo yum -y install powershell. Fortunately, it wasn’t as difficult as building from source. Here’s what I did:
Install the dependencies
1sudo yum install -y curl libunwind libicu libcurl openssl libuuid.x86_64
Download the installation script
This script just fetches the tarball and extracts it to /opt/microsoft/powershell
1wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/docker/InstallTarballPackage.sh
Set the script to be executable
1chmod +x InstallTarballPackage.sh
Run the script, specifying the PowerShell version (6.0.1) and package tarball as the arguments:
1sudo ./InstallTarballPackage.sh 6.0.1 powershell-6.0.1-linux-x64.tar.gz
If you want to install a specific version (like the latest), then refer to the releases on the PowerShell repo.
Run PowerShell!
The command is pwsh, as in “Present Working SHell” (clever points). Be sure to use sudo, as it does require root privileges:
1sudo pwsh
Get Your .NET On
The whole point of this exercise was to see if I could use .NET to perform DNS name resolution without any of the cmdlets in the Windows-only dnsclient module. Did it work? Let’s see.
1PowerShell v6.0.1
2Copyright (c) Microsoft Corporation. All rights reserved.
3
4https://aka.ms/pscore6-docs
5Type 'help' to get help.
6PS /home/ec2-user> [System.Net.Dns]::GetHostAddresses("benpiper.com").IPAddressToString
752.205.213.4
Yes indeed! Of course, I can still use the usual PowerShell tricks to extract just the data I want:
1PS /home/ec2-user> [System.Net.Dns]::GetHostByName("pluralsight.com") | Select-Object AddressList
2
3AddressList
4 -----------
5 {54.213.174.143, 35.164.44.204, 52.39.160.43}
I can also drill down to pick out just the first IP address in the list:
1PS /home/ec2-user> ([System.Net.Dns]::GetHostByName("pluralsight.com")).AddressList[0].IpAddressToString
254.213.174.143
Run it again, and I get a different address:
1PS /home/ec2-user> ([System.Net.Dns]::GetHostByName("pluralsight.com")).AddressList[0].IpAddressToString
252.39.160.43
Looks like round-robin DNS! But will this command work cross-platform? Let’s try it on my Windows 10 machine:
1PS C:\Users\admin> ([System.Net.Dns]::GetHostByName("pluralsight.com")).AddressList[0].IpAddressToString
235.164.44.204
Yes! This is exactly why I chose PowerShell. The same command that works on Linux also works on Windows, which makes it perfect for an OS-agnostic course.