Powershell and Azure on MacOS
As a mobile developer 90% of the time I work on a mac. Visual Studio for Mac is an awesome tool to create Xamarin mobile apps. More than not mobile apps are using services. You can use .NET Core to create APIs but deploying them isn’t so easy on a mac. Sure you can use the Azure CLI, but if you are working with mixed teams Powershell seems to be king to deploy towards Azure.
About a year ago Microsoft made Powershell available for MacOs. But that doesn’t mean you can use all of the Modules that are available. Until a few days ago you couldn’t use AzureRM. Now that the AzureRM.NetCore is no longer in preview, it’s time to explain how you can use the power of Powershell and AzureRM on your mac.
As I couldn’t get the AzureRM modules working on an older version you need to make sure you run the latest version of Powershell for Mac. You can download a .pkg package from https://github.com/PowerShell/PowerShell. Installing is as simple as any other package. Just follow the wizard.
After installing the package you can verify the version of Powershell by opening a terminal window and start Powershell.
# Start powershell just by typing: powershell # Once you have the powershell prompt check the version with: $psversiontable
The version I have is v6.0.0-beta4. I couldn’t make AzureRM work with version v6.0.0-beta3 so make sure you have at least version v6.0.0-beta4.
Installing AzureRM modules
Before installing modules you’ll need to know in which directory you want to store your modules. Open a Powershell and verify the path where you can store your modules
# start a powershell session powershell # verify the path for modules $env:PSModulePath
My path looks like:
I will store my modules in:
To install the AzureRM modules you’ll need to open up a terminal and login as a root user. The instructions come from: https://www.powershellgallery.com/packages/AzureRM.Netcore/0.9.1
# login with a admin user su myrootuser # start a powershell session powershell Save-Module -Name AzureRM.Netcore -Path /usr/local/microsoft/powershell/6.0.0-beta.4/Modules Install-Module -Name AzureRM.Netcore
The path provided in the latest command corresponds with the path you found via $env:PSModulePath
Using the AzureRM modules
You can close the admin/root session and start a session as normal user.
Verify that the AzureRM modules are available
# start a powershell session powershell # list available modules Get-Module -ListAvailable
If you try to execute the Login-AzureRmAccount login command to login on Azure and start executing your scripts.
What if the AzureRM modules are not available?
When starting a Powershell session, not all modules are loaded to save memory. Depending on how you installed or stored the modules they won’t be available by default.
So if the Login-AwureRmAccount is not available for you, you can auto load the AzureRm modules when starting a new Powershell session. You can do that by changing your profile.
Search for your profile:
# start a powershell session powershell # find your profile file $profile
My profile is located on:
If it doesn’t exist you can simply create it.
You can simply import a module by adding “Import-Module MyModuleName” in the file. Because AzureRM has multiple modules you can add them in one command
Get-Module -ListAvailable AzureRm.* | Import-Module
Authenticate to Azure via Powershell
Now you can authenticate via Powershell so can start executing the powershell scripts in your Xamarin solution.
# start a powershell session powershell # authenticate Login-AzureRmAccount WARNING: To sign in, use a web browser to open the page https://aka.ms/devicelogin and enter the code LOWVPMJ51 to authenticate.
Now open up a browser and go to https://aka.ms/devicelogin, provide the code that you got in your powershell session and you’re good to go!
Microsoft made powershell and the AzureRM Modules available on MacOS. It will allow us to automate our backend deploys for our Xamarin mobile apps without switching to Windows.