Azure CLI Samples for Azure DevTest Labs
This article includes sample bash scripts built for Azure CLI for Azure DevTest Labs.
Script | Description |
---|---|
Create and verify a virtual machine (VM) | Creates a Windows VM with minimal configuration. |
Start a VM | Starts a VM. |
Stop and delete a VM | Stops and deletes a VM. |
Prerequisites
To run this sample, install the latest version of the Azure CLI. To start, run az login
to create a connection with Azure.
Samples for the Azure CLI are written for the bash
shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change
elements of the script.
If you don't have an Azure subscription, create an Azure free account before you begin.
All of these scripts have the following prerequisite:
- A lab. The script requires you to have an existing lab.
Create and verify availability of a VM
This Azure CLI script creates a virtual machine in a lab. The VM created based on a marketplace image with SSH authentication. The script then verifies that the VM is available for use.
resourceGroupName='<Resource group in which lab exists>'
location='<Location in which the lab exists>'
labName="<Name of the lab>"
vmName="<Name for the VM>"
vmImageName="<Name of the image. For example: Ubuntu Server 16.04 LTS>"
vmSize="<Size of the image. For example: Standard_DS1_v2>"
# Create a resource group
az group create \
--name $resourceGroupName \
--location $location
# Create a VM from a marketplace image with ssh authentication
az lab vm create
--lab-name $labName
--resource-group $resourceGroupName
--name $vmName
--image $vmImageName
--image-type gallery
--size $vmSize
--authentication-type ssh
--generate-ssh-keys
--ip-configuration public
# Verify that the VM is available
az lab vm show
--lab-name sampleLabName
--name sampleVMName
--resource-group sampleResourceGroup
--expand 'properties($expand=ComputeVm,NetworkInterface)'
--query '{status: computeVm.statuses[0].displayStatus, fqdn: fqdn, ipAddress: networkInterface.publicIpAddress}'
This script uses the following commands:
Command | Notes |
---|---|
az group create | Creates a resource group in which all resources are stored. |
az lab vm create | Creates a VM in a lab. |
az lab vm show | Displays the status of the VM in a lab. |
Start a VM
This Azure CLI script starts a virtual machine in a lab.
resourceGroupName='<Resource group in which lab exists>'
labName="<Name of the lab>"
vmName="<Name for the VM>"
# Start the VM
az lab vm start
--lab-name $labName
--name $vmName
--resource-group $resourceGroupName
This script uses the following commands:
Command | Notes |
---|---|
az lab vm start | Starts a VM in a lab. This operation can take a while to complete. |
Stop and delete a VM
This Azure CLI script stops and deletes a virtual machine in a lab.
Caution
Deleting VMs and labs is permanent, and cannot be undone.
resourceGroupName='<Resource group in which lab exists>'
labName="<Name of the lab>"
vmName="<Name for the VM>"
# Stop the VM
az lab vm stop
--lab-name $labName
--name $vmName
--resource-group $resourceGroupName
# Delete the VM
az lab vm delete
--lab-name $labName
--name $vmName
--resource-group $resourceGroupName
This script uses the following commands:
Command | Notes |
---|---|
az lab vm stop | Stops a VM in a lab. This operation can take a while to complete. |
az lab vm delete | Deletes a VM in a lab. This operation can take a while to complete. |
Clean up deployment
Run the following command to remove the resource group, VM, and all related resources.
Caution
Deleting the resource group for the lab is permanent, and cannot be undone. This will remove ALL resources under the group and can not be restored.
az group delete --name $resourceGroupName