VMs and Availability Sets can be created using PowerShell as well. Besides the traditional PowerShell, you can also use the Azure Cloud Shell to create your Availability Set. By using the Azure Cloud Shell, you are basically using PowerShell from inside the browser. Inside the Azure Cloud Shell, Windows users can opt for PowerShell and Linux users can opt for Bash. You can open the Azure Cloud Shell from the Azure Portal, as shown in the following screenshot:
Azure Cloud Shell
To create two VMs and add them to an Availability Set, add the following PowerShell statements to Azure Cloud Shell or Windows PowerShell (note that when using the Azure Cloud Shell, you don't have to log in):
Login-AzureRmAccount
If necessary, select the right subscription, shown as follows:
Select-AzureRmSubscription -SubscriptionId "********-****-****-****-***********"
Create a resource group:
New-AzureRmResourceGroup -Name PacktPubPS -Location WestEurope
Now, create an Availability Set:
New-AzureRmAvailabilitySet -Location WestEurope -Name AvailabilitySet02 -ResourceGroupName PacktPubPS -Sku Aligned -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 2
Next, we need to create the two VMs and add them to the Availability Set. This is done by setting the -AvailabilitySetId parameter to the ID of the Availability Set. When running this script, you will be prompted for the username and password for your VM, as shown in the following snippet:
$availabilitySet = Get-AzureRmAvailabilitySet -ResourceGroupName PacktPubPS -Name AvailabilitySet02
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name PacktSubnet -AddressPrefix 192.168.1.0/24
$vnet = New-AzureRmVirtualNetwork -ResourceGroupName PacktPubPS -Location WestEurope -Name PacktVnet -AddressPrefix 192.168.0.0/16 -Subnet $subnetConfig
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name PacktNetworkSecurityGroupRuleRDP -Protocol Tcp -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow
$nsg = New-AzureRmNetworkSecurityGroup -Location WestEurope -Name PacktSecurityGroup -ResourceGroupName PacktPubPS -SecurityRules $nsgRuleRDP
# Apply the network security group to a subnet
Set-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name PacktSubnet -NetworkSecurityGroup $nsg -AddressPrefix 192.168.1.0/24
# Update the virtual network
Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
for ($i=1; $i -le 2; $i++)
{
$pip = New-AzureRmPublicIpAddress -ResourceGroupName PacktPubPS -Location WestEurope -Name "$(Get-Random)" -AllocationMethod Static -IdleTimeoutInMinutes 4
$nic = New-AzureRmNetworkInterface -Name PacktNic$i -ResourceGroupName PacktPubPS -Location WestEurope -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
# Specify the availability set
$vm = New-AzureRmVMConfig -VMName PacktVM$i -VMSize Standard_D2_v3 -AvailabilitySetId $availabilitySet.Id
$vm = Set-AzureRmVMOperatingSystem -ComputerName myVM$i -Credential $cred -VM $vm -Windows -EnableAutoUpdate -ProvisionVMAgent
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
New-AzureRmVM -ResourceGroupName PacktPubPS -Location WestEurope -VM $vm
}