Google
 

Friday, July 18, 2014

Using Azure command line tools in Windows batch files

I have an Azure IaaS environment for development and experiments. The environment consists of 3 machines. I wanted to automate starting and shutting down the VMs in this environment.
I used Azure Command-Line Interface to create two batch files, one to start the environment and the other to stop it.

The scripts looks like:

azure vm start VMName1
azure vm start VMName2
azure vm start VMName3


and

azure vm shutdown VMName3
azure vm shutdown VMName2
azure vm shutdown VMName1


But when I executed the batch files, only the first command is executed then the script just terminates even if the command is successful. I had no clue why!!

I asked my friend @mShady who thankfully pointed me to this stack overflow thread: http://stackoverflow.com/a/4036937. The answer tells: "you must call another script using the call command so it knows to return back to your script after the called script completes. Try prepending call to all commands."

So I added call to the batch files:

call azure vm start VMName1
call azure vm start VMName2
call azure vm start VMName3

and

call azure vm shutdown VMName3
call azure vm shutdown VMName2
call azure vm shutdown VMName1


And it worked.

The documentation for call mentions that it "Calls one batch program from another without stopping the parent batch program".


After checking, I found that the azure command is a batch file that internally calls node. And that was the issue.
The path of the batch file (which is in the path environment variable to be visible anywhere) is: "C:\Program Files\Microsoft SDKs\Windows Azure\CLI\wbin\azure.cmd"

3 comments:

Unknown said...

Awesome - thanks!

Anonymous said...

Hi,
How can Azure copy(AzCopy) command be defined in the batch file?

Hesham A. Amin said...

@Anonymous:
If you have the command in your PATH environment variable then you should be able to call it normally from the batch file. Did you face any specific challenges with it?