Microsoft Azure (AZ-104): PowerShell Script / Azure CLI

goay xuan hui
3 min readJun 13, 2021


Create a Virtual Network

  1. Create a resource group
az group create 
--name CreateVNetQS-rg
--location eastus

2. Create a virtual network

az network vnet create 
--name myVNet
--resource-group CreateVNetQS-rg
--subnet-name default

3. Create a virtual machine

az vm create 
--resource-group CreateVNetQS-rg
--name myVM1
--image UbuntuLTS
--public-ip-address myPublicIP-myVM1

Manage Azure disks with Azure PowerShell

  1. Create the initial configuration with New-AzDiskConfig.
$diskConfig = New-AzDiskConfig 
-Location "EastUS"
-CreateOption Empty
-DiskSizeGB 128

2. Create the data disk with the New-AzDisk command.

$dataDisk = New-AzDisk 
-ResourceGroupName "myResourceGroupDisk"
-DiskName "myDataDisk"
-Disk $diskConfig

3. Get the virtual machine that you want to add the data disk to with the Get-AzVM command.

$vm = 
-ResourceGroupName "myResourceGroupDisk"
-Name "myVM"

4. Add the data disk to the virtual machine configuration with the Add-AzVMDataDisk command.

$vm = Add-AzVMDataDisk     
-VM $vm
-Name "myDataDisk"
-CreateOption Attach
-ManagedDiskId $dataDisk.Id
-Lun 1

5. Update the virtual machine with the Update-AzVM command.

-ResourceGroupName “myResourceGroupDisk”
-VM $vm

Change the capacity of a scale set

az vmss scale 
--resource-group myResourceGroup
--name myScaleSet
--new-capacity 5

Create a custom role

$role = Get-AzRoleDefinition "Virtual Machine Contributor"
$role.Id = $null
$role.Name = "Virtual Machine Operator"
$role.Description = "Can monitor and restart virtual machines."
-Role $role

Move resource to different subscription

Move-AzResource -DestinationSubscriptionId
NOT Move-VMResource!!!

Create a public load balancer to load balance VMs

  1. Create a new resource group
-Name 'CreatePubLBQS-rg'
-Location 'eastus'

2. Create a public IP address

$publicIp = Get-AzPublicIpAddress 
-Name 'myPublicIP'
-ResourceGroupName 'CreatePubLBQS-rg'
az network public-ip create
--resource-group CreatePubLBQS-rg
--name myPublicIP
--sku Standard

3. Create load balancer frontend configuration and assign it with the public IP address

$feip = New-AzLoadBalancerFrontendIpConfig 
-Name 'myFrontEnd'
-PublicIpAddress $publicIp
az network lb create
--resource-group CreatePubLBQS-rg
--name myLoadBalancer
--sku Standard
--public-ip-address myPublicIP
--frontend-ip-name myFrontEnd
--backend-pool-name myBackEndPool

Manage Storage Account

  1. List IP Rules
-ResourceGroupName "myresourcegroup"
-AccountName "mystorageaccount").IPRules

2. Add a network rule for an individual IP address

-ResourceGroupName "myresourcegroup"
-AccountName "mystorageaccount" -IPAddressOrRange ""

3. Create Storage Account

az storage account create 
--name <storage-account>
--resource-group <resource-group>

--location <location>
--sku Standard_ZRS
--encryption-services blob

4. Create Container

az storage container create 
--account-name <storage-account>
--name <container>

--auth-mode login

Create Custom Image

az vm deallocate
--resource-group <>
--name demovm
az vm generalize
--resource-group <>
--name demovm
az image create
--resource-group <>
--name <imageName>
--source demovm

Create DNS Record

az network dns record-set a add-record 
--resource-group myresourcegroup
--record-set-name www
az network dns record-set a add-record
--resource-group myresourcegroup
--record-set-name "@"

To create a record set in the apex of the zone (in this case, “”), use the record name “@”.

A domain apex is the “root” level of your domain. For example, let’s say you just purchased We’d call that the “domain apex”, meaning that is the “root” of the hierarchy of domain names.

Create a Role From a File Containing JSON Descripton

az role definition create 
--role-definition @newrole.json



goay xuan hui

A food lover, a cyber security enthusiast, a musician and a traveller, so you will see a mix of different contents in my blog. ☺️