Simple bash scripting for “azure” cli

The azure cli is a great tool for easily working with Windows Azure interactively. You can also use it for scripting  right from bash/gitbash. The bash shell offers a bunch of tools at your disposal like grep, awk, sed, and xargs which are designed specifically to make your scripting life easier.

If you don’t know about these tools, here is a one liner for each.

  • grep takes incoming text and pulls out lines that contain a matching pattern or regular expression. It returns a file that has the matched lines. It can also do an inverse match returning all lines that don’t match.
  • awk takes delimited text, parses the delimited data, and feeds the values into a script. 
  • sed takes an incoming stream of characters and feeds the stream to a script.
  • xargs takes an incoming set of arguments and invokes a command passing in the args.
  • cut takes incoming text and lets you cut specific text blocks out.

To script with our cli, you will call “azure” and pipe the results into one or more of the tools I listed (or similar tools) which applies some filtering or transformation and ultimately makes additional “azure” calls. From my own playing around, I found that a combination of “grep” and “awk” were basically sufficient because our cli returns data in a columnar format. However any of the tools above can work.

Below is a list of some common simple scripting scenarios for websites, which includes the command and a small explanation of what each is doing. This should get you started and you can easily take the pattern and apply to other parts of our cli.

Disclaimer, I am not at all a bash expert, so there probably are better ways to do what I am showing you.

Stop all running sites

  • Lists out all websites.
  • Pipes to grep which then matches on all lines that have the word “Running”. This filters out anything but the lines for running sites.
  • Pipes to awk and then for each row calls “azure site stop” passing the site name (the second column that was parsed)

Start all stopped sites

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Stopped”. This filters out anything but the lines for stopped sites.
  • Pipes to awk and then for each row calls “azure site start” passing the site name.

Delete all running or stopped sites

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Stopped” or “Running”.
  • Pipes to awk and then for each row calls “azure site delete” passing the site name. –q is passed as well in order to override the prompt for deleting.

Clear and add an app setting to every site

  • Lists out all websites.
  • Pipes to grep which then matches on all sites that have a status of “Running”.
  • Pipes to awk and then for each row calls “azure site config clear foo ” passing the site name. This removes the setting if it previously exists.
  • Retrieves running sites again and then pipes to awk to call “azure site config add foo=bar” passing the site name. This adds the foo setting to all sites.

What about the other tools?

When I first started playing around I look at xargs and cut. The approach is basically using cut to pull the site name and then pass it to xargs. I almost went with that approach until I realized it was not necessary as awk will parse delimiters. I am much happy not having to use char positions as that is more brittle.

Here’s the code

  • Pipes the list of websites to cut which extracts the text between char positions 10-32 for each line that is piped in.
  • Pipes the resulting list to xargs which invokes “azure site start” passing in the arguments. By default xargs will pass all the lines to the the command, “-n 1” says only pass a max of one line to the command, thus “azure” will get called for each line.

Getting azure calls to run in the background.

If you run all the previous calls, you will see that everything executes synchronously meaning sites are deleted one after the other. You can force them to run in the background, though when you do the console output may look a little wacky. To do that you just add an ‘&’ at the end of the shell command, for example below I have modified the delete sites script to run in the background.

Merry scripting

This is just the tip of the iceberg of what you can once you start scripting the cli. My coworker Josh also just blogged about how you can use the same approach for managing your Azure Mobile Service scripts. If you are using Mobile Services, be sure to check it out!

If you’ve got any cool scripts you wanna share, post links in the comments. Also let if there are specific things you’d like to see to improve the scripting experience.

Thanks to @xpaulbettsx, @subdigital@davidmfoley, and @einaros for their scripting help!

Happy Holidays!

This entry was posted in azure, cli. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Korayem

    Turns out the BASH library I was using Cygwin and even GIT Bash weren’t compiled with respect to Windows OS. I tried Win-BASH and worked like a charm. Download from here

    From their website:

    “Unlike other bash ports for Windows (e.g. the cygwin bash ), the win-bash needs no special environment or DLLs. There is just one binary and that’s it.
    win-bash is based on nt_bash which was an early bash port for Windows NT started by Mountain Math Software some years ago. As far as I know, the nt_bash port project stopped in alpha status and has never been finished.
    The goal of the win-bash project is to finish the port to Windows and provide a fully-functional bash.exe binary for Windows NT and derived systems. win-bash can be used as an input shell, as well as an interpreter to run UN*X shell scripts.”

  • Korayem

    Thanks for sharing this. It makes managing Azure much faster.

    I am getting this weird error with any GAWK command in Windows Command Line

    sh: azure: command not found

    I have correct paths setup. Running azure command itself works fine.

    After a lot of digging, I found that gawk system() looks up AWKPATH and not PATH so it doesn’t find Azure CLI.

    I then added Azure CLI to PATH C:Program Files (x86)Microsoft SDKsAzureCLIwbin and changed your command to

    azure sites list | grep ‘Running’ | awk ‘{system(“azure.cmd site stop “$2)}’

    this time Azure treats me as if I am not logged in and asks me to login to Azure!

    Any clues?