This article is focuses on how new latest AZ PowerShell module can be used to provision a Windows virtual machine on Azure.
You would need Azure account in order to execute these script. If you don’t have one, you can create a free Azure account here.
If you are not planning for any Azure certification, I would strongly recommend to at least go for AZ-203 certification which is about knowing Azure products from developer perspective.
Please refer my blog article below and it should help you get prepared for the exam.
Plan your AZ-203 certification today !
Let’s Get Started for creating a windows VM on Azure.
Setup few variables
Before logging in to Azure and starting VM creation, let’s setup few variables.
Resource Group – All Azure resources are organized in Resource Groups. We are going to create new resource i.e. Virtual Machine inside resource group of this name.
Location – The Azure region in which we want to place the new resource
VM Name – to specify the name of virtual machine
$resourceGroup = "Fully-Configured-Windows-VM-WE" $location = "westeurope" $vmName = "MyFirstVM"
Login to Azure
Below command would help to login to Azure. Once this command is executed, it would open a popup where you can enter your email id and password for logging into Azure. Once you are logged in PowerShell would show command prompt and you will be able to type the next command.
# Login to Azure Connect-AzAccount
Create credentials object
This statement would show a popup where you can enter the username and password. This same username and password can be used by you to login to the Virtual Machine.
We are going to use this object in subsequent steps.
# Create user credential object # which would be used for Virtual machine $cred = Get-Credential ` -Message "Enter a username and password for VM."
Create Resource Group
This statement would create the resource group. The virtual machine would be placed inside this resource group.
If this resource group is deleted, all resources inside this resource groups would be deleted.
# Create a resource group New-AzResourceGroup ` -Name $resourceGroup ` -Location $location
This would create a new SubNet and it would also reserve the IP address space.
# Create a subnet configuration $subnetConfig = New-AzVirtualNetworkSubnetConfig ` -Name mySubnet ` -AddressPrefix 192.168.1.0/24
Create virtual network
The virtual network allows virtual machines to securely communicate with one another.
# Create a virtual network $vnet = New-AzVirtualNetwork -ResourceGroupName $resourceGroup ` -Location $location ` -Name MYvNET ` -AddressPrefix 192.168.0.0/16 ` -Subnet $subnetConfig
Create public IP and DNS configuration
This command to reserve a static public IP address. Even if you boot multiple times, the static IP does not change. This IP can be used to connect with virtual machine using RDP.
# Create a public IP address and specify a DNS name $pip = New-AzPublicIpAddress -ResourceGroupName $resourceGroup ` -Location $location ` -Name "mypublicdns$(Get-Random)" ` -AllocationMethod Static ` -IdleTimeoutInMinutes 4
Create Network Security Group & Inbound rule for 3389 Port
The Network Security Group has rules which either allow or deny the traffic inbound to virtual network or outbound from virtual network.
The 3389 port is the port for allowing remote desktop.
# Create an inbound network security group rule for port 3389 $nsgRuleRDP = New-AzNetworkSecurityRuleConfig ` -Name myNetworkSecurityGroupRuleRDP ` -Protocol Tcp ` -Direction Inbound ` -Priority 1000 ` -SourceAddressPrefix * ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange 3389 ` -Access Allow # Create a network security group $nsg = New-AzNetworkSecurityGroup ` -ResourceGroupName $resourceGroup ` -Location $location ` -Name myNetworkSecurityGroup ` -SecurityRules $nsgRuleRDP # Create a virtual network card # and associate with public IP address and NSG $nic = New-AzNetworkInterface ` -Name myNic ` -ResourceGroupName $resourceGroup ` -Location $location ` -SubnetId $vnet.Subnets.Id ` -PublicIpAddressId $pip.Id ` -NetworkSecurityGroupId $nsg.Id
Create virtual machine configuration
This block selects the virtual machine size, virtual machine operating system image, and network interface card. This object is then used to create the actual virtual machine.
# Create a virtual machine configuration $vmConfig = New-AzVMConfig -VMName $vmName ` -VMSize Standard_D1 | ` Set-AzVMOperatingSystem ` -Windows -ComputerName ` $vmName -Credential $cred | ` Set-AzVMSourceImage ` -PublisherName MicrosoftWindowsServer ` -Offer WindowsServer ` -Skus 2016-Datacenter ` -Version latest | ` Add-AzVMNetworkInterface -Id $nic.Id
Create the virtual machine
This command uses all the configurations done till now and creates the virtual machine.
You will have to wait around 5 minutes of time to get the virtual machine created.
# Create a virtual machine New-AzVM -ResourceGroupName $resourceGroup ` -Location $location ` -VM $vmConfig
Login to this virtual machine
You can login to this virtual machine using below PowerShell command
# Login to virtual machine Get-AzRemoteDesktopFile ` -ResourceGroupName $resourceGroup ` -Name $vmName ` -Launch
Something to remember…
If you have created this VM only for learning purpose, please note that there is cost associated with every running virtual machine. So, please do not forget to delete your newly created resource group and all its contents by running below PowerShell commands
#Removes all resources within this resource group Remove-AzResourceGroup -Name $resourceGroup
I hope you enjoyed this article. Please do not forget to comment and let me know if this information helped you.