Mixing Azure Resource Management and Service Management Modes in a PowerShell Script

Suppose you need to come up with a script that does work both on Azure Service Management (ASM) and Azure Resource Management (ARM) stacks, however, in Azure PowerShell module, they seem to be exclusive, all of the tutorials say you need to issue

to switch, and there are name clashes with some commandlets, say with Get-AzureStorageAccount. What to do?

Well, if you dig a little bit deeper and see how the Azure Module is loaded, it is basically a shortcut that calls a script (ShortcutStartup.ps1) installed by the WebPI package. It sits on “C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services” and does some tricks.
It first loads in the module called Azure.psd1 (thus if you do a Get-Module, it gets listed, after the module is loaded. Please search for the lifetime of a PowerShell module, I do not want to get into the details of it here), and if you want to switch to ARM mode, the commandlet itself unloads the active module (please see the source at https://github.com/Azure/azure-powershell/blob/dev/src/Common/Commands.Profile/SwitchAzureMode.cs) and imports the other. And if you look around a little bit, you will see the ARM module’s manifest (AzureResourceManager.psd1) in your PowerShell Azure module installation.

Once you find those, you can come up with a script that loads both of the modules manually, like following:

Ok, now we have both of the modules, but how to resolve clashing names? Well, for that PowerShell language comes to rescue, you simply prepend the module’s name to the commandlet’s name, delimited with a ‘\’ character. Example, for accessing the ARM version of Get-AzureStorageAccount (thus displaying V2 of storage accounts)

or for ASM storage account commandlet: