Latest posts by Michael Pietroforte (see all)
- Evernote backup to Dropbox - Tue, Jan 9 2018
- Install PowerShell Core and the Azure module (AzureRM) on a Mac - Tue, Dec 26 2017
- New wiki doc about changing the PowerShell console colors - Thu, Dec 21 2017
I played with PowerShell Core on macOS shortly after it became available. I very much liked the idea of being able to work with PowerShell on my Mac without needing to launch a virtual machine with Windows. However, I was disappointed when I noticed important modules were not working. After stumbling a couple of times along the installation path, my Mac now "loves" Azure.
Install PowerShell Core on macOS ^
My first attempt to install PowerShell Core appeared to fail. Like with the alpha version, I just downloaded the PKG package because it is the fasted method. All you have to do is double-click the file and follow the wizard. This time macOS noticed the package as trustworthy, and I didn't have to go to the security settings to approve the installation. The wizard ended with an encouraging "The installation was successful."
I then launched a Terminal, entered "powershell" and boom: -bash: powershell: command not found. This had worked with the PowerShell Core alpha. Strange, I thought. How can this happen with a release candidate? Interestingly, the package adds an app called "PowerShell" to the Application folder. However, when I clicked the corresponding icon in Launchpad, a hex file opened in my text editor (Sublime if you want to know). Weird!
# Download the powershell '.tar.gz' archive
curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-rc.2/powershell-6.0.0-rc.2-osx-x64.tar.gz
# Create the target folder where powershell will be placed
sudo mkdir -p /usr/local/microsoft/powershell/6.0.0-rc.2
# Expand powershell to the target folder
sudo tar zxf /tmp/powershell.tar.gz -C /usr/local/microsoft/powershell/6.0.0-rc.2
# Set execute permissions
sudo chmod +x /usr/local/microsoft/powershell/6.0.0-rc.2/pwsh
# Create the symbolic link that points to pwsh
sudo ln -s /usr/local/microsoft/powershell/6.0.0-rc.2/pwsh /usr/local/bin/pwsh
When I saw the last the command, it dawned on me what the problem with the package installation was. Someone at Microsoft felt "powershell" was too much type-type and renamed that thing to "pwsh." That's fine with me. However, why not rename PowerShell to "posh?" Sounds almost as if PowerShell were the high-class edition of "bash." 😉
Install the AzureRM module on PowerShell Core ^
I was tempted to try the package installation again, but then I was too curious to find out how powerful PowerShell (or pwsh) is on a Mac. (Let me know if this worked for you.) Of course, the first thing I tried to install was the Azure module:
Don't try this! Even though the installation went through without error messages, it didn't work. After I typed Login-AzureRmaccount, I received this:
Login-AzureRmaccount: The term 'Add-AzureRmAccount' is not recognized as the name of a cmdlet, function, script file, or operable program.
This is not what I expected. I wasn't ready to give up yet though. I couldn't believe Microsoft offered a release candidate for PowerShell Core without support for Azure. So I removed the module, googled a little, and found some blog posts that claimed to explain how to install AzureRM on PowerShell Core. I failed again in a similar way. Many PowerShell Core articles out there no longer work. Finally, I found this one. This worked. Not exactly as described, but it worked.
The main thing on PowerShell Core is that you don't install the AzureRM module but AzureRM.NetCore:
Lesson learned. PowerShell Core is not really PowerShell, and many modules will have to be adapted. Nevertheless, if you try the above command at a freshly installed PowerShell Core prompt, you will probably receive this error message (after Install-Module downloads the module):
PackageManagement\Install-Package : Administrator rights are required to install or update.
Fair enough. I should have known this. But I am on a Mac, so no right-click, Run as Administrator thingy. Instinctively, I entered sudo Install-Module AzureRM.NetCore. To my surprise I received a password prompt. Huh? PowerShell learned how to sudo? Not so fast:
sudo: install-module: command not found
What a pity! I would have liked this. But this works: Exit the PowerShell console by typing exit and then enter sudo pwsh. Now you have a PowerShell console with administrator rights, and you can install modules.
After entering Install-Module AzureRM.NetCore again, PowerShell Core warned me I'm installing modules from an untrusted repository. Microsoft's not trustworthy? Who says that?
Microsoft's documentation claims you then have to import the module with Import-Module AzureRM.Netcore. However, for me it worked without that, probably because I restarted the PowerShell console.
After I entered Login-AzureRmAccount, it greeted me with a longwinded warning that explained why Microsoft wants to collect data about my usage of cmdlets. I don't know about you, but I don't feel like sharing all of my failed and embarrassing PowerShell attempts. And hey, didn't I just install software from an untrustworthy source I now should trust with my most personal data?
So I answered the suggestive request "Select Y to enable data collection Y/N" with a distinct "N." I almost had to laugh when I received another WARNING: You choose not to participate in Microsoft Azure PowerShell data collection. Okay, I've been warned and I understand the risks of not sending all of my data to Microsoft.
Shortly afterward, another WARNING appeared informing me I had to open a web browser to the URL https://aka.ms./devicelogin and enter an authentication code. This warning I took seriously, and seconds after I followed the instructions, my Mac and Azure were finally friends. I find it odd (to say the least) I need to open a web browser to sign in from a console.
The next thing I need to do is change the color of these ugly yellow warning messages. I somehow think I will see these WARNINGS more often on my Mac in the months to come.