How to install open-source PowerShell on Linux VM in Azure

When you think of “open-source software,” Microsoft may not be the first name that jumps into your mind. But the world’s largest software company is, in fact, getting more and more open source. GitHub’s “The State of the Octoverse 2016” report found that Microsoft is the organization with the most open-source contributors. Who would have thought that a few years back? Also nearly one in three virtual machines hosted in Azure are running Linux.

One of the more exciting developments is Microsoft’s recent decision to open source its popular PowerShell. Meanwhile, it seemed that an article about Linux VMs hosted in Azure was long overdue. And since I’m a big fan of PowerShell, I decided to combine both. The open-source feature allows you to install PowerShell on multiple Linux distributions. This includes CentOS and Ubuntu as well as Mac OS.  I will show you how to deploy PowerShell on a Linux instance using ARM Templates and custom-script extensions.

So how do we get started?

Azure Resource Manager template or PowerShell?

Because I am a big fan of Azure Resource Manager templates, I will use a template to deploy the Linux VM. The difference between PowerShell and templates is simple but very important. The so-called ARM templates use a declarative style to deploy resources in Azure. PowerShell, on the other hand, has an imperative style. The main advantage of templates is that I can easily deploy multiple instances and/or resources by just describing them. Afterward, I can share them with my colleagues in a GIT repository or somewhere different. These templates are very good for sharing and for bigger environments.

Note: Microsoft and community members share a lot of quickstart templates on Github, which you can find here.

PowerShell is great to deploy simple VMs very quickly. On the other hand, when it comes to bigger and more complex environments, it’s often not my first tool of choice. Your mileage may vary, but in my case I’m feeling more comfortable using the templates. Nevertheless, can you still use PowerShell to deploy the template, by using the New-AzureRmResourceGroupDeployment cmdlet. Or configure everything using PowerShell only.

Building the template

Creating the template is quite straightforward and easy because you can use an editable quickstart template. To make it even easier, I will share my template with you so you don’t need to create it from scratch. Because I use a template, I need to define it in a declarative environment, and here it is getting kind of tricky. In my case I didn’t want to build multiple templates for each supported Linux distribution, and instead use only one template. The problem:  While working with templates, imperative statements like the “If condition” are not possible.

But I need something like an “If condition” to configure the image of the VM, since it changes massively from CentOS to Ubuntu. This is where complex variables are your friend. The following picture shows the definition of the variable called imageSettings. This variable consists of multiple tiers and under tiers, each representing another operating system.

complex variables

The real magic, though, is happening in the definition of the variable currentImageSettings. This variable combines another variable (imagesettings) with a parameter (operatingSystem). Now let us have a look at the parameter:

parameter: operation system

The allowed values for the parameter have the same name as the possible configuration setting in the variable imagesettings, so the variable currentImageSettings will be dynamically filled with the data chosen in the parameter. This means:

IF the user chooses “Ubuntu14” as their operating system, THEN the variable currentImageSettings would be filled like imagesettings.Ubuntu14.

And there we have our needed If condition! The variables publisher, offer, and sku will be set accordingly to the chosen operation system.

Custom-script extension

Whenever you want to fully automate a software installation during the deployment or provisioning phase, the use of the custom script extension is the way to go. While there are other possibilities like Azure Automation in combination with DSC, the custom-script extension is free of charge and easy to configure. To automatically install PowerShell on the Linux host is actually very straightforward.

The PowerShell team, especially Andrew Schwartzmeyer, made it incredible easy to install. They developed a script bash script that automatically checks your operating system and installs PowerShell in the right manner. So the only thing you are left with is to point to the script, which is also published on Github, within you template. The reference can look like something like this:

Install Powershell script

And there you have it. You now have the tools to build your own Linux box in Azure with PowerShell on it.

Save the template in Azure

It’s relatively recent that you can save templates in Azure itself without the use of an external GIT repository as I did before. If you want to have it more secure you just need to click on more services in the Azure Portal. Then search for “templates” and you have the option to add a template as seen below:

Save template in Azure

Afterward, you can easily deploy or edit your template directly within Azure.

Deploy or edit template in Azure

That’s all there is to it! So take advantage of Microsoft’s generosity and install the newly open-sourced PowerShell in your Linux VMs hosted in Azure.

Photo credit: Pexels

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top