Azure CLI – Creating a Virtual Machine

In my previous article, I described the process for creating a Virtual Machine in the Azure Portal. A colleague asked me to show them what the Azure CLI script might look like to create the same Virtual Machine, therefore I have quickly put the following together.

For more information on Azure CLI please click here.

Scope

The following script is being utilised to configure a single VM with an additional Data Disk. The sizing of the VM is based around keeping costs to a minimum and the location is in a UK data centre.

Along with this, a single port 3389 will be opened to allow an RDP connection to the VM.

Azure CLI Script

az vm create \
    --resource-group jonnychipz-rg \
    --name DC02 --location uksouth \
    --image win2016datacenter \
    --size Standard_DS1_v2 \
    --priority regular \
    --admin-username azureadmin \
    --vnet-name jonnychipz-vnet \
    --subnet servers \
    --tags owner=jonnychipz 

az vm open-port \
    --port 3389 \
    --resource-group jonnychipz-rg \
    --name DC02

az vm disk attach \
    --resource-group jonnychipz-rg \
    --vm-name DC02 --name ADDS2 \
    --size-gb 32 \
    --sku Standard_LRS \
    --caching none \
    --new

Output from Script Run

As can be seen from running this script, I have not included an Admin Password parameter, mainly because I didn’t want to store a password in clear text, therefore when you run the CLI Script, you will be prompted to enter a password for your local Admin user account.

Also, I have called this VM ‘DC02’ and in effect this VM is now configured similarly to the VM we created through the portal.

One last point to note is that some of the resources I have referenced in this above script were already in place, i.e. the VNET/Subnet as well as the resource group etc. If you are completing this from scratch, then you would want to accommodate those resources as well.

  • Please note that Public IP detail has been removed.
Created VM in Azure Portal
Running DC2 Virtual Machine in Azure

So, we have explored the Creation of a Virtual Machine utilising the Azure CLI, I do hope this has been of use to you, please like and subscribe to my blog and other social media outlets to keep up to date with my musings!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s