First, we need to retrieve the virtual network information and store it in a variable by running the following cmdlet:
$VNet = Get-AzureRmVirtualNetwork -Name PSVNet -ResourceGroupName PacktPub
The same also applies for the NSG, which is performed by running the following cmdlet:
$NSG = Get-AzureRmNetworkSecurityGroup -Name PSNSG -ResourceGroupName PacktPub
Next, we need to create a public IP by running the following cmdlet:
$PIP = New-AzureRmPublicIpAddress -ResourceGroupName PacktPub -Location WestEurope -AllocationMethod Dynamic -Name PacktPubVMPIP
After that, we have to create an NIC for the VM by running the following cmdlet:
$NIC = New-AzureRmNetworkInterface -ResourceGroupName PacktPub -Location WestEurope -Name PacktPubVMNIC -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PIP.Id -NetworkSecurityGroupId $NSG.Id
Now we're done with the VM networking part. The next step is to specify the VM's settings and configurations by running the following cmdlet:
$VMConfig = New-AzureRmVMConfig -VMName PacktPubVMPS -VMSize Standard_D1_v2 | Set-AzureRmVMOperatingSystem -Windows -Credential (Get-Credential) -ComputerName PackPubVMPS | Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2016-Datacenter -Version latest | Add-AzureRMVMNetworkInterface -Id $NIC.ID
You can get all the sizes of the VM at Get-AzureRMVMSize -Location, where you can also specify the location in which you are going to build your VM.
You will be prompted to enter the VM credentials when you run the preceding cmdlet.
Finally, to create the VM, you need to run the following cmdlet:
New-AzureRmVM -ResourceGroupName PacktPub -Location WestEurope -VM $VMConfig