Creating a Windows Virtual Machine Programmatically

Get-AzVMImageSku -Location

Install Azure Powershell

run the Azure-Cli in powershell in interactive mode

az interactive

you can run this command in both local powershell and in cloud shell.

Finding the pre-built image names

the name that we need for building the vm’s programaticaly, the problem is not all the images are listed on the “marketplace” we can solve this problem by finding all the images name via command line Get-AzVMImageSku -Location "EastAsia" -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" | where Skus -Match "2019"

# First list all the publisher names that are available in that location
Get-AzVMImagePublisher -Location "centralindia"
# then list all the offers from that publisher
Get-AzVMImageOffer -Location "centralindia" -PublisherName "MicrosoftWindowsDesktop" | Select offer
# Fill in your chosen offer name and list the SKUs:
Get-AzVMImageSku -Location "centralindia" -PublisherName "MicrosoftWindowsDesktop" -Offer "windows-10-2004-vhd-client-prod-stage"

Get-AzVMImage -Location "centralindia" -PublisherName "MicrosoftWindowsDesktop" -Offer "windows-10-2004-vhd-client-prod-stage" -Sku "20h1-ent-g2" | Select Version

# try this command first to find the image, it will feel like this command is not running,
# it will take upto 5 minutes or maybe more to return the results
az vm image list -f "Windows-10" --all

az vm image list --location centralindia
az vm image list-skus --location centralindia --offer windows-10-2004-vhd-client-prod-stage --publisher MicrosoftWindowsDesktop

 az vm image show --location centralindia --offer windows-10-2004-vhd-client-prod-stage --publisher MicrosoftWindowsDesktop --sku 20h1-ent-g2
## use the run to build vm, privide the urn as image parameter
az vm image show --location centralindia --urn MicrosoftWindowsDesktop:Windows-10:20h1-ent-g2:19041.508.2009070256

az vm image list-skus -l centralindia -f windows-10-2004 -p MicrosoftWindowsDesktop

Finding the name of regions

For example in drop down list we have “(Asia Pacific) East Asia”, drop the in prenthesis whatever it is and remove the space in “East Asia” to “EastAsia”

Configuring the output format of Azure-cli

# change the output from json to table format

az configure

# type yes
# type 3 for table formate
# No
# No
# enter

Check the list of resource groups

az group list

az group list --query "[?location=='centralindia']"

#Installing for other OSs is available here:

#Get Your subscription Details
az account list --all

#Login interactively and set a subscription to be the current active subscription
az login && az account set --subscription "Demonstration Account"

## Time to create the Windows VM ##
#1 - Create a resource group, then query the list of resource groups in our subscription
az group create --name "pluralsight-autolab-rg" --location "CentralIndia"

az group list -o table

#2 - Create virtual network (vnet) and Subnet
az network vnet create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-vnet-1" --address-prefix "" --subnet-name "pluralsight-autolab-subnet-1" --subnet-prefix ""

# Creating new subnet in existing vnet and resource group
az network vnet subnet create -g "pluralsight-autolab-rg" --vnet-name "pluralsight-autolab-vnet-1" -n "pluralsight-autolab-subnet-2" --address-prefixes ""

az network vnet list -o table

#3 - Create public IP address
az network public-ip create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-rg-pip-1"

# Public ip for second subnet
az network public-ip create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-rg-pip-2"

#4 - Create network security group, so we can have seperate security policies
az network nsg create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-nsg-1"

#5 - Create a virtual network card and associate with public IP address and NSG
az network nic create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-nic-1" --vnet-name "pluralsight-autolab-vnet-1" --subnet "pluralsight-autolab-subnet-1" --network-security-group "pluralsight-autolab-nsg-1" --public-ip-address "pluralsight-autolab-rg-pip-1"

# created second nic for second virtual machine, by keep resource group, NSG and VNet same. only changed subnet and public ip
az network nic create --resource-group "pluralsight-autolab-rg" --name "pluralsight-autolab-nic-2" --vnet-name "pluralsight-autolab-vnet-1" --subnet "pluralsight-autolab-subnet-2" --network-security-group "pluralsight-autolab-nsg-1" --public-ip-address "pluralsight-autolab-rg-pip-2"

#6 - Create a virtual machine
az vm create --resource-group "pluralsight-autolab-rg" --name "DC1" --location "CentralIndia" --nics "pluralsight-autolab-nic-1" --image "MicrosoftWindowsServer:WindowsServer:2019-Datacenter-Core:latest" --admin-username "ahmadkakarr" --admin-password "P@kistan2541"

## created second vm in different subnet
az vm create --resource-group "pluralsight-autolab-rg" --name "S1" --location "CentralIndia" --nics "pluralsight-autolab-nic-2" --image "MicrosoftWindowsServer:WindowsServer:2019-Datacenter-Core:latest" --admin-username "ahmadkakarr" --admin-password "P@kistan2541"

az vm create --resource-group "pluralsight-autolab-rg" --name "Client1" --location "CentralIndia" --nics "pluralsight-autolab-vnet-win10" --image "MicrosoftWindowsDesktop:Windows-10:20h1-ent-g2:19041.508.2009070256" --admin-username "ahmadkakarr" --admin-password "P@kistan2541"
#7 - Open port 3389 to allow RDP traffic to host, use the public ip only in the case of testing services or in production env, other for testing or just want to access the vm use the "bastion" service.
az vm open-port --port "3389" --resource-group "pluralsight-autolab-rg" --name "DC1"
az vm open-port --port "3389" --resource-group "pluralsight-autolab-rg" --name "S1"

az vm list-ip-addresses --name "DC1"  --output table

#Use Remote Desktop to connect to to this VM

#Let's create a Windows VM with a little less code...using PowerShell Splatting.

#Create PSCredential object, this will be used for the Windows username/password
$password = ConvertTo-SecureString 'password123412123$%^&*' -AsPlainText -Force
$WindowsCred = New-Object System.Management.Automation.PSCredential ('demoadmin', $password)

#We're using the Image parameter, for a list of images look here

#Use tab complete to help find your image name, enter into the Terminal
New-AzVm -Image

$vmParams = @{
    ResourceGroupName = 'psdemo-rg'
    Name = 'psdemo-win-2'
    Location = 'centralus'
    Size = 'Standard_D1'
    Image = 'Win2016Datacenter'
    PublicIpAddressName = 'psdemo-win-2-pip-1'
    Credential = $WindowsCred
    VirtualNetworkName = 'psdemo-vnet-2'
    SubnetName = 'psdemo-subnet-2'
    SecurityGroupName = 'psdemo-win-nsg-2'
    OpenPorts = 3389
New-AzVM @vmParams

Get-AzPublicIpAddress `
    -ResourceGroupName 'psdemo-rg' `
    -Name 'psdemo-win-2-pip-1' | Select-Object -ExpandProperty IpAddress

#Launch RDP session to new VM...