How to Create a Virtual Machine Image in Azure (CLI)

Azure-cloud-iconFrom command prompt of the VM you want to snap the image:

C:\windows\system32\sysprep\sysprep

  1. In the system preparation tool box, make sure the following are checked:
  • System Cleanup Action: Enter System Out-Of-Box Experience (OOBE)
  • Generalize: is checked
  • Shutdown Options: Shutdown

In the Cloud Shell, run the following command to reallocate the virtual machine:

az vm deallocate \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–name MyWindowsVM

Now run the following command to generalize the virtual machine:

az vm generalize \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–name MyWindowsVM

Create a new Virtual Machine Image:

Run the following command to create a new VM image

az image create \
–name MyVMIMage \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–source MyWindowsVM

Create a new VM with the image we just created:

az vm create \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–name MyVMFromImage \
–image MyVMImage \
–admin-username azureuser

Open port 80 to allow web traffic through:

az vm open-port \
–name MyVMFromImage \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–port 80

Find new VMs public IP address:

echo http://$(az vm list-ip-addresses \
–resource-group Learn-7d66e471-e9a1-44fa-b74d-944d3acd382b \
–name MyVMFromImage \
–query “[].virtualMachine.network.publicIpAddresses[*].ipAddress” \
–output tsv)

Posted in Azure, Microsoft and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *