Saturday 31 August 2019

Setting Up Az Module to Write IaC for Azure

Microsoft has deprecated the AzureRM PowerShell module and introduced new Az PowerShell module for Azure. Bug fixes and support will be available for AzureRM module until December 2020. However, it is better starting migrating any AzureRM based PowerShell scripts which you have written to achieve Infrastructure as Code (IaC) for Azure. For this purpose, you can setup the Azure Az Module in your machine while allowing the AzureRM commands to still work using alias. Let’s look at the steps of setting up Azure Az Module in a machine where Azure RM is already setup.
As the first step we need to check version of Azure RM installed in the machine. For this execute below command.
Get-InstalledModule -Name AzureRM -AllVersions

Then check if AzureRM is installed via msi by looking for it in Settings --> Apps in Windows 10. If it is windows 8 or below you can look at Control Panel -- > Uninstall Programs.


If AzureRM msi found as installed uninstall it using uninstall programs or from Settings -- > App in Windows 10. Below are the options for windows versions to look for the Azure RM msi.


· Windows 10 Start > Settings > Apps
· Windows 7
· Windows 8 Start > Control Panel > Programs > Uninstall a program


As shown in above image if the AzureRM msi is not found the assumption is that you have used Install-Module -Name AzureRM to install AzureRM module. Then you can uninstall AzureRM module by executing command below.
Uninstall-Module -Name AzureRM -AllVersions -Force


Before installing Azure Az module check the version of PowerShell in your machine. PowerShell version should be version 5.1 or higher. To check version of PowerShell, execute below command. If the PowerShell version is not 5.1 or higher update it following instructions here.
$PSVersionTable.PSVersion
Then check if .NET 4.7.2 or higher available in your machine. To determine the version of use instructions here. If .NET 4.7.2 r later is not available in you machine install it.

Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' |  Get-ItemPropertyValue -Name Release | Foreach-Object { $_ -ge 461808}


To install the Az Module execute the below command.
Install-Module -Name Az -AllowClobber -Scope CurrentUser
Or scope it to all users using below. However, it is recommended to use current user as other users of the machine may not prefer to use Az module as of yet.
Install-Module -Name Az -AllowClobber -Scope AllUsers


You may get
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change
its InstallationPolicy value by running the Set-PSRepository cmdlet.
Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):


Select Yes to all and continue. Close all PowerShell windows and open a new PowerShell window. We can enable Azure RM commands to be used with Az module preventing AzureRM based scripts from failing. For this run the below command. If you have not closed all PowerShell windows and opened a new one below command will complain that both Azure RM and AZ modules are detected.
Enable-AzureRmAlias -Scope CurrentUser

Or to enable it for local machine.
Enable-AzureRmAlias -Scope LocalMachine

Available scopes are Local, Process, CurrentUser and LocalMachine.
However, if you run below command you still see some AzureRM modules in machine.
Get-InstalledModule


To remove all modules based on AzureRM execute below Az command.
Uninstall-AzureRM

Now you can see all AzureRM modules are uninstalled.

But still you can use AzureRM commands in your scripts since the Enable-AzureRmAlias is done previously.

once you migrate all your scripts to use new commands in Az modue you can disable the AzureRM alias commands by executing below. Available scopes are Local, Process, CurrentUser and LocalMachine.
Disable-AzureRmAlias -scope currentuser

No comments:

Popular Posts