Task 2:

Deploy an Azure VM running Windows Server 2019 Datacenter into an existing availability set by using Azure PowerShell

  • From the Azure Portal, start a PowerShell session in the Cloud Shell pane.

Note: If this is the first time you are launching the Cloud Shell in the current Azure subscription, you will be asked to create an Azure file share to persist Cloud Shell files. If so, accept the defaults, which will result in creation of a storage account.

New Resource and Resource group:

  • In the Cloud Shell pane, run the following commands:
$vmName = 'az104-vm1'
$vmSize = 'Standard_DS1_v2'

Note: This sets the values of variables designating the Azure VM name and its size

  • In the Cloud Shell pane, run the following commands:
$resourceGroup = Get-AzResourceGroup -Name '<some value>'
$location = $resourceGroup.Location

# <some value> -> replace this with the value (resource group name) of your choice

Note: These commands set the values of variables designating the target resource group and its location

  • In the Cloud Shell pane, run the following commands:
$availabilitySet = Get-AzAvailabilitySet -ResourceGroupName $resourceGroup.ResourceGroupName -Name 'az104-avset0'
$vnet = Get-AzVirtualNetwork -Name '<some value>' -ResourceGroupName $resourceGroup.ResourceGroupName
$subnetid = (Get-AzVirtualNetworkSubnetConfig -Name 'subnet0' -VirtualNetwork $vnet).Id

# <some value> -> replace this with the value (vnet name) of your choice

Note: These commands set the values of variables designating the availability set, virtual network, and subnet into which you will deploy the new Azure VM

  • In the Cloud Shell pane, run the following commands:
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroup.ResourceGroupName -Location $location -Name "$vmName-nsg"
$pip = New-AzPublicIpAddress -Name "$vmName-ip" -ResourceGroupName $resourceGroup.ResourceGroupName -Location $location -AllocationMethod Dynamic
$nic = New-AzNetworkInterface -Name "$($vmName)$(Get-Random)" -ResourceGroupName $resourceGroup.ResourceGroupName -Location $location -SubnetId $subnetid -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

Note: These commands create a new network security group, public IP address, and network interface that will be used by the new Azure VM

  • In the Cloud Shell pane, run the following commands:
$adminUsername = 'LabAdmin'
$adminPassword = '<some value>'
$adminCreds = New-Object PSCredential $adminUsername, ($adminPassword | ConvertTo-SecureString -AsPlainText -Force)

Note: These commands set the values of variables designating credentials of the local Administrator account of the new Azure VM

  • In the Cloud Shell pane, run the following commands:
$publisherName = 'MicrosoftWindowsServer'
$offerName = 'WindowsServer'
$skuName = '2019-Datacenter'

Note: These commands set the values of variables designating the properties of the Azure Marketplace image that will be used to provision the new Azure VM

  • In the Cloud Shell pane, run the following command:
$osDiskType = (Get-AzDisk -ResourceGroupName $resourceGroup.ResourceGroupName)[0].Sku.Name

Note: This command sets the values of a variable designating the operating system disk type of the new Azure VM

  • In the Cloud Shell pane, run the following commands:
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $availabilitySet.Id
Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential $adminCreds
Set-AzVMSourceImage -VM $vmConfig -PublisherName $publisherName -Offer $offerName -Skus $skuName -Version 'latest'
Set-AzVMOSDisk -VM $vmConfig -Name "$($vmName)OsDisk_1$(Get-Random)" -StorageAccountType $osDiskType -CreateOption fromImage
Set-AzVMBootDiagnostic -VM $vmConfig -Disable

Note: These commands set up the properties of the Azure VM configuration object that will be used to provision the new Azure VM, including the VM size, its availability set, network interface, computer name, local Administrator credentials, the source image, the operating system disk, and boot diagnostics settings.

  • In the Cloud Shell pane, run the following command:
New-AzVM -ResourceGroupName $resourceGroup.ResourceGroupName -Location $location -VM $vmConfig -AsJob

Note: This command initiates deployment of the new Azure VM

  • In the Cloud Shell pane, run the following command:
Get-Job

Note: This command shows the deployment status of the new Azure VM

Continue to Task 3..... (under preparation)