Showing posts with label Azure CLI Commands. Show all posts
Showing posts with label Azure CLI Commands. Show all posts

Wednesday, August 21, 2019

Common Azure CLI commands

The Azure CLI is a command-line tool providing a great experience for managing Azure resources. The Azure command-line interface (CLI) is probably the second most-used tool after the web portal. The CLI runs on any platform and covers a wide variety of actions.To install Azure CLI , refer to https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest 
The very first command will be to log in to Azure:
az login 
 The login operation has a unique session identifier. Once you sign in with this session ID, the CLI receives a notification on its back channel. The notification contains a JWT access token.
From this point on, the access token is used by most other CLI commands to access Azure Management REST API. API uses OAuth protocol where the access token is passed in the Authroisation  HTTP header.  As the CLI is a short-lived program: it runs for the duration of a single command execution and then quits. The process dies, so there’s no way to keep access tokens in memory between the executions. To avoid continually asking for user credentials, the CLI keeps its state on disk. If you go to the ~/.azure/ directory (%HOMEPATH%/.azure/ in Windows command-line) you can find json and other files. The access token has a limited lifespan aorund 60 minutes. To avoid requiring to login after access expiration, there is another powerful token—a refresh token. Whenever an access token expires, CLI goes to the authentication service, presents the refresh token, and asks for a new access token. The lifetime of a refresh token is longer, and it’s managed on the service side.

Create VMs

TaskAzure CLI commands
Create a resource groupaz group create --name myResourceGroup --location eastus
Create a Linux VMaz vm create --resource-group myResourceGroup --name myVM --image ubuntults
Create a Windows VMaz vm create --resource-group myResourceGroup --name myVM --image win2016datacenter

Manage VM state

TaskAzure CLI commands
Start a VMaz vm start --resource-group myResourceGroup --name myVM
Stop a VMaz vm stop --resource-group myResourceGroup --name myVM
Deallocate a VMaz vm deallocate --resource-group myResourceGroup --name myVM
Restart a VMaz vm restart --resource-group myResourceGroup --name myVM
Redeploy a VMaz vm redeploy --resource-group myResourceGroup --name myVM
Delete a VMaz vm delete --resource-group myResourceGroup --name myVM

Get VM info

TaskAzure CLI commands
List VMsaz vm list
Get information about a VMaz vm show --resource-group myResourceGroup --name myVM
Get usage of VM resourcesaz vm list-usage --location eastus
Get all available VM sizesaz vm list-sizes --location eastus

Disks and images

TaskAzure CLI commands
Add a data disk to a VMaz vm disk attach --resource-group myResourceGroup --vm-name myVM --disk myDataDisk --size-gb 128 --new
Remove a data disk from a VMaz vm disk detach --resource-group myResourceGroup --vm-name myVM --disk myDataDisk
Resize a diskaz disk update --resource-group myResourceGroup --name myDataDisk --size-gb 256
Snapshot a diskaz snapshot create --resource-group myResourceGroup --name mySnapshot --source myDataDisk
Create image of a VMaz image create --resource-group myResourceGroup --source myVM --name myImage
Create VM from imageaz vm create --resource-group myResourceGroup --name myNewVM --image myImage


For all of the other commands, please refer to https://docs.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest