Working with Azure Resource Manager Cross-platform with PowerShell Core

This is a brief introduction to connecting to your Azure Cloud subscription using Azure Resource Manager module (AzureRM) in both Windows PowerShell and PowerShell Core. You could install both AzureRM and AzureRM.Netcore modules in the same Windows System. Or, when using Linux, just install the AzureRM.Netcore module.

Azure Resource Manager Installation

In Windows Powershell, use the following command to *install AzureRM (or Azure.Netcore) from the PowerShell Gallery:

[sourcecode language=”powershell”]
## – Install AzureRM from the PowerShell Gallery:
Install-Module -Name AzureRM -Repository PSGallery;

[/sourcecode]

*Note: Keep in mind, the console need to be open with elevated permission or it won’t install the module(s).

In Linux, use “sudo pwsh” to start PowerShell with admin pernmission.

[sourcecode language=”bash”]
$ sudo pwsh

[/sourcecode]

Then, enter the following PowerShell command to install AzureRM.Netcore module:

[sourcecode language=”powershell”]
## – Install AzureRM for PowerShell Core:
Install-Module -Name-AzureRM.Netcore -Repository PSGallery;

[/sourcecode]

To see the list of installed AzureRM in PowerShell, execute the following command:

1. AzureRM Modules. Using the following command will provide the list of modules *loaded:
[sourcecode language=”powershell”]
## – List of installed AzureRM modules:
Get-Module -ListAvailable AzureRM `
| Select name, version, path;

[/sourcecode]

2. In Linux, AzureRM.Netcore Modules. Using the following command will provide the list of modules loaded:

[sourcecode language=”powershell”]
## – List of installed AzureRM.Netcore modules:
Get-Module -ListAvailable AzureRM.Netcore `
| Select name, version, path;

Get-Module -ListAvailable *.Netcore `
| Select name, version, path;

[/sourcecode]


*Note: If you see multiple installation of the same modules, but different versions, you’ll need to pick the right version. Or, this could lead to experiencing having issues when executing azure commands.

You’ll notice that not all Windows PowerShell AzureRM modules has been ported to PowerShell Core yet.

Connecting Linux to Azure Cloud

Here’s where fun begin! We all know in Windows Systems, for automation purpose, we need to create the a certificate key so you can connect to our Azure subscription. But in Linux, is different!

The following steps will connect a Linux System to an Azure subscription:

1. One time only, I use the following command to login to Azure:
[sourcecode language=”powershell”]
## – Login to Azure:
Login-AzureRmAccount;

[/sourcecode]

Executing this command, will have you do a few things:

1. Ask if you want to “enable data collection” (Y/N). (pick one)

2. At this point the command will pause waiting for you to open the browser.

3. Open link provided (copy/paste): https://aka.ms/devicelogin

4. The proceed to enter the code:

5. Enter you Azure email account and password:

6. At the end, you will be asked to close the page and the command will end executiion connecting to Azure.

Try to execute a few AzureRM commands to make sure the connection was successful:

[sourcecode language=”powershell”]
## – Testin Azure connections:
Get-AzureRMSubscription;

Get-AzureRMVM -ResourceGroupName AZRESOURCEEAST01 -Status;

[/sourcecode]

Saving Connection Profile for Automation

After verifying connectivity to Azure, use the following command to save your Azure Subscription Profile in a *Json file. Just give it a location and save the file. This will speed up the *connection step.

[sourcecode language=”powershell”]
## – Save Azure subscription profile: (one time or as needed)
Save-AzureRmContext -Path “/home/username/AzureSubs/lx_ARMprofile.json”;

[/sourcecode]

Next time around connecting to Azure, use the Import command to connect:

[sourcecode language=”powershell”]
Import-AzureRmContext -Path “/home/username/AzureSubs/lx_ARMprofile.json”;

[/sourcecode]

These step works in both Windows PowerShell and PowerShell Core.

Word of Caution

Now, during my venture of moving from Azure Classic modules to Azure Resource Manager (AzureRM) module, I ended hitting the wall. The user experience was rough! It was all due to have duplication modules and different versions, installed in mutliples locations.

There’s a chance that you will encounter the same issues: mostly obsolete/deprecated commands and/or parameters no longer valid. Now, specially if you install Visual Studio, the Azure SDK option might be already an older version.

So, what’s the solution? Definitely, a cleanup is necessary. Then, trust the PowerShell Galery repository to get the latest versions. More in this issue in upcoming post.

For now, don’t stop learning PowerShell!