Automate mobile services with command-line tools
This topic shows you how to use the Windows Azure command-line tools to automate the creation and management of Windows Azure Mobile Services. This topic shows you how to install and get started using the command-line tools and use them to perform the following Mobile Services tasks:
When combined into a single script or batch file, these individual commands automate the creation, verification, and deletion process of a mobile service.
To use the Windows Azure command-line tools to manage Mobile Services, you need a Windows Azure account that has the Windows Azure Mobile Services feature enabled.
This topic covers a selection of common administration tasks supported by the Windows Azure command-line tools. For more information, see Windows Azure command-line tools documentation.
Install the Windows Azure Command-Line Tools
The following list contains information for installing the command-line tools, depending on your operating system:
To test the installation, type azure at the command prompt. When the installation is successful, you will see a list of all the available azure commands.
How to download and import publish settings
To get started, you must first download and import your publish settings. Then you can use the tools to create and manage Azure Services. To download your publish settings, use the account download command:
azure account download
This opens your default browser and prompts you to sign in to the Management Portal. After signing in, your .publishsettings file is downloaded. Note the location of this saved file.
Next, import the .publishsettings file by running the following command, replacing <path-to-settings-file> with the path to your .publishsettings file:
azure account import <path-to-settings-file>
You can remove all of the information stored by the import command by using the account clear command:
azure account clear
To see a list of options for account commands, use the -help option:
azure account -help
After importing your publish settings, you should delete the .publishsettings file for security reasons. For more information, see How to install the Windows Azure Command-Line Tools for Mac and Linux. You are now ready to begin creating and managing Windows Azure Mobile Services from the command line or in batch files.
How to create a mobile service
You can use the command-line tools to create a new mobile service instance. While creating the mobile service, you also create a SQL Database instance in a new server.
The following command creates a new mobile service instance in your subscription, where <service-name> is the name of the new mobile service, <server-admin> is the login name of the new server, and <server-password> is the password for the new login:
azure mobile create <service-name> <server-admin> <server-password>
The mobile create command fails when the specified mobile service exists. In your automation scripts, you should attempt to delete a mobile service before attempting to recreate it.
How to list existing mobile services in a subscription
The following command returns a list of all the mobile services in a Windows Azure subscription:
azure mobile list
This command also shows the current state and URL of each mobile service.
How to delete an existing mobile service
You can use the command-line tools to delete an existing mobile service, along with the related SQL Database and server. The following command deletes the mobile service, where <service-name> is the name of the mobile service to delete:
azure mobile delete <service-name> -a -q
By including -a and -q parameters, this command also deletes the SQL Database and server used by the mobile service without displaying a prompt.
Note If you do not specify the -q parameter along with -a or -d, execution is paused and you are prompted to select delete options for your SQL Database. Only use the -a parameter when no other service uses the database or server; otherwise use the -d parameter to only delete data that belongs to the mobile service being deleted.
How to create a table in the mobile service
The following command creates a table in the specified mobile service, where <service-name> is the name of the mobile service and <table-name> is the name of the table to create:
mobile table create <service-name> <table-name>
This creates a new table with the default permissions, application, for the table operations: insert, read, update, and delete.
The following command creates a new table with public read permission but with delete permission granted only to administrators:
mobile table create <service-name> <table-name> -p read=public,delete=admin
The following table shows the script permission value compared to the permission value in the Windows Azure Management Portal.
| Script value | Management Portal value |
public | Everyone |
application (default) | Anybody with the application key |
user | Only authenticated users |
admin | Only scripts and admins |
The mobile table create command fails when the specified table already exists. In your automation scripts, you should attempt to delete a table before attempting to recreate it.
How to list existing tables in a mobile service
The following command returns a list of all of the tables in a mobile service, where <service-name> is the name of the mobile service:
azure mobile table list <service-name>
This command also shows the number of indexes on each table and the number of data rows currently in the table.
How to delete an existing table from the mobile service
The following command deletes a table from the mobile service, where <service-name> is the name of the mobile service and <table-name> is the name of the table to delete:
azure mobile table delete <service-name> <table-name> -q
In automation scripts, use the -q parameter to delete the table without displaying a confirmation prompt that blocks execution.
How to register a script to a table operation
The following command uploads and registers a function to an operation on a table, where <service-name> is the name of the mobile service, <table-name> is the name of the table, and <operation> is the table operation, which can be read, insert, update, or delete:
azure mobile script upload <service-name> table/<table-name>.<operation>.js
Note that this operation uploads a JavaScript (.js) file from the local computer. The name of the file must be composed from the table and operation names, and it must be located in the table subfolder relative to the location where the command is executed. For example, the following operation uploads and registers a new insert script that belongs to the TodoItems table:
azure mobile script upload todolist table/todoitems.insert.js
The function declaration in the script file must also match the registered table operation. This means that for an insert script, the uploaded script contains a function with the following signature:
function insert(item, user, request) {
...
} For more information about registering scripts, see Mobile Services server script reference.