mac.jpg

Starting and Stopping Multiple Azure VM’s from a Mac Terminal

We recently started a project in which ensuring we kept costs of Azure VM’s as low as possible was a real concern. Since I am a Mac user (stop snickering), the Azure CLI tools seemed like an obvious choice. I just wanted a simple way to check the current status of the virtual machines in a specific Azure Subscription, and then start to shutdown the virtual machines as needed. I agree with Scott Hansleman, and thought I would save myself and others some keystrokes. #sharingiscaring

Using the Azure CLI from a Mac Terminal

The first thing I found when searching was Using the Azure CLI for Mac. This was pretty helpful and gave me some basic commands, but didn’t give me the simple path of shutting down all the virtual machines that were running at once. This did give me the basic commands that let me check the status of the virtual machines, and stop and start the machines as well. By the way, this page has a bunch of info I plan on diving into at a later date.

To start, you’ll need to install the Azure CLI tools. This is a pretty straight forward install using npm.

npm install azure-cli -g

Once you have the tools, you’ll need to get your subscription settings. I have to run the following:

azure account download

This will open a browser and you’ll need to select the subscription for which you want the settings file. Once the subscription information was downloaded, running

azure account import [settings file]

will set the current account.

The Basic Commands

Once the Azure CLI tools are installed, the basic commands to list, stop and start the virtual machines are pretty simple

azure vm list

azure vm start vm-name

azure vm shutdown vm-name

Great. Almost there.

Painful Unix Memories

Unfortunately, even though I am now a Mac user, I still have to bang my head against the wall to remember Unix commands and pipe operations. Bad memories of awk and sed from many years ago. Towards the end of the install, the Azure CLI tools post, there was a section about “understanding results” which did something very similar to what I needed. This example was using xargs and something I had never heard of called jsawk. The xargs didn’t give me any flashbacks, so I was ok with that, but the jsawk made me twitch a little. Turns out, the curl installation of jsawk from the GitHub repo failed for me, but I was too close to stop!

A link in the jsawk readme mentioned using Homebrew, so the following installed jsawk and the dependencies I was missing. Back in business!

brew search jsawk
brew install jsawk

The Final Solution

Finally, all the pieces are in place. Now I can list the virtual machines, filter them, and more. This is a little deeper than I want to go here, but the jsawk documentation is really great. With the ability to filter and then use xargs to call another azure command, the final solution was just a simple change based on the example towards the end of the install the Azure CLI tools post. Here they are:

Shutdown some Virtual Machines

azure vm list --json | jsawk -n 'out(this.VMName)' | xargs -L 1 azure vm shutdown

Start all of the Virtual Machines

azure vm list --json | jsawk -n 'out(this.VMName)' | xargs -L 1 azure vm start

There are still some things I want to do, specifically create a shell script to enable some other options and arguments, but this might be a blog post for another day. Hopefully this will help someone else and save some keystrokes.

And BTW – I do realize this would be very easy in PowerShell. I’m just sort of new to the Mac. #justsaying

Get-AzureVM | Where-Object {$_.Status -like "Ready*"} | ForEach-Object { Stop-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force }

Let me know what you think or if this was helpful.

 

Pete SkellyStarting and Stopping Multiple Azure VM’s from a Mac Terminal

Join the conversation

This site uses Akismet to reduce spam. Learn how your comment data is processed.