Auto-Shutdown with Azure CLI
This post will show you how to configure a Microsoft Azure virtual machine to auto-shutdown using Azure CLI.
Install Azure CLI
The Azure CLI is a command-line tool for managing Azure resources from Windows, Mac and Linux
Install on Windows
To install Azure CLI on Windows is very easy.
You can use this link to download the installer or you can launch this PowerShell script with administrator rights:
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'
Check the installation
az --version
Sign in to Azure
az login
This method is interactive and you have to open a browser and navigate to this link: https://aka.ms/devicelogin.
Enter the authorization code displayed in the terminal and the credentials for your Azure Tenant.
Manage auto-shutdown for VM.
az vm auto-shutdown [--email]
[--ids]
[--location]
[--name]
[--off]
[--resource-group]
[--subscription]
[--time]
[--webhook]
Example
Create auto-shutdown schedule for a VM.
az vm auto-shutdown -g MyResourceGroup -n MyVm --time 1730 --email "foo@bar.com" --webhook "https://example.com/"
Delete auto-shutdown schedule for a VM.
az vm auto-shutdown -g MyResourceGroup -n MyVm --off
Example for specific resource group (Off)
az vm auto-shutdown --ids $(az vm list -g MyResourceGroup --query "[].id" -o tsv) --off
Optional Parameters
The email recipient to send notifications to (can be a list of semi-colon separated email addresses).
–ids
One or more resource IDs (space-delimited). It should be a complete resource ID containing all information of ‘Resource Id’ arguments. You should provide either –ids or other ‘Resource Id’ arguments.
–location -l
Location. Values from: az account list-locations
. You can configure the default location using az configure --defaults location=<location>
.
–name -n
The name of the Virtual Machine. You can configure the default using az configure --defaults vm=<name>
.
–off
Turn off auto-shutdown for VM. Configuration will be cleared.
–resource-group -g
Name of resource group. You can configure the default group using az configure --defaults group=<name>
.
–subscription
Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID
.
–time
The UTC time of day the schedule will occur every day. Format: hhmm. Example: 1730.
–webhook
The webhook URL to which the notification will be sent.
–debug
Increase logging verbosity to show all debug logs.
–help -h
Show this help message and exit.
–only-show-errors
Only show errors, suppressing warnings.
–output -o
Output format.
–query
JMESPath query string. See http://jmespath.org/ for more information and examples.
–subscription
Name or ID of subscription. You can configure the default subscription using az account set -s NAME_OR_ID
.
–verbose
Increase logging verbosity. Use –debug for full debug logs.