Deploy Azure Virtual Machines With Accelerated Networking Enabled Using Powershell

Setting the context

Accelerated Networking is a great feature provided by Microsoft to boost network performance and CPU utilization for Azure Virtual Machines. However, it has been made available for a couple of Virtual Machine series and sizes where there is a real need for Network performance and those VMs are meant for high CPU utilization tasks.

In the previous article (refer Accelerated Networking For Azure Virtual Machines), the concept of Accelerated Networking is described in depth along with step by step guide to enable Accelerated Networking using Azure Portal. This article dives further into the PowerShell code that will help to enable Accelerated Networking for Azure Virtual Machines. This article should be treated as a continuation of the previous article and it is highly recommended to go through the previous article and then get into this one.

Enable Accelerated Networking for Azure Virtual Machines using PowerShell

In the real world, most of the Azure deployments for the client are done using PowerShell scripting. Hence, it is highly essential to have a deep understanding of Azure resources deployment using PowerShell. And here Accelerated Networking is enabled for Virtual Machines using PowerShell script.

Let us begin by creating a Resource Manager and all necessary resources for Azure Virtual Machine and then create a Virtual Machine with Accelerated Networking enabled.

Let us start by signing in to Azure. On executing the below PowerShell code a pop up will open up and will prompt for keying in credentials and will log in to Azure on providing correct credentials.

  1. # Log In to Azure  
  2. LogIn-AzureRMAccount  

Now, let us create a Resource Manager. Below PowerShell code will create a Resource manager named rg-demo-accnetworking in location eastus.

  1. # Create Resource Group  
  2. New-AzureRmResourceGroup -Name "rg-demo-accnetworking" -Location "eastus"  

Now, let us create a Virtual Network named vnet-demo-accnetworking in the location eastus with IP address space as 10.8.0.0/16. It will have a subnet called subnet-demo-accnetworking that has address space as 10.8.1.0/24.

  1. # Create subnet configuration  
  2. $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name "subnet-demo-accnetworking" -AddressPrefix "10.8.1.0/24"  
  3.  
  4. # Create Virtual Network along with subnet  
  5. # Splat all parameters together for better readability. We are bundling all parameters  
  6. # for the command together to adhere to PowerShell coding best practices and have a better  
  7. # code readability  
  8. $paramVNet = @{  
  9.   ResourceGroupName = "rg-demo-accnetworking"  
  10.   Location = "eastus"  
  11.   Name = "vnet-demo-accnetworking"  
  12.   AddressPrefix = "10.8.0.0/16"  
  13.   Subnet = $subnet  
  14. }  
  15. $vnet = New-AzureRmVirtualNetwork @paramVNet  

Create a Network Security Rule that will allow RDP connection. A Network security Rule configuration is created with name rg-demo-nsg-allow-rdp and a Network Security Group nsg-demo-accnetworking is created with the Network Security Rule configuration created.

  1. # Create Network security rule configuration that will allow RDP connection  
  2. # Splat all parameters together for better readability. We are bundling all parameters  
  3. # for the command together to adhere to PowerShell coding best practices and have a better  
  4. # code readability  
  5. $paramNetworkSecurityGroupRuleConfig = @{  
  6.   Name = "rg-demo-nsg-allow-rdp"  
  7.   Description = "Enable RDP connection"  
  8.   Access = "Allow"  
  9.   Protocol = "TCP"  
  10.   Direction = "Inbound"  
  11.   Priority = 100  
  12.   SourceAddressPrefix = "*"  
  13.   SourcePortRange = "*"  
  14.   DestinationAddressPrefix = "*"  
  15.   DestinationPortRange = 3389  
  16. }  
  17. $rdpConfig = New-AzureRmNetworkSecurityRuleConfig @paramNetworkSecurityGroupRuleConfig  
  18.  
  19. # Create Network security group configuration with security rule configuration defined previously  
  20. # Splat all parameters together for better readability. We are bundling all parameters  
  21. # for the command together to adhere to PowerShell coding best practices and have a better  
  22. # code readability  
  23. $paramNetworkSecurityGroup =@{  
  24.   Name = "nsg-demo-accnetworking"  
  25.   ResourceGroupName="rg-demo-accnetworking"  
  26.   Location = "eastus"  
  27.   SecurityRules = $rdpConfig  
  28. }  
  29. $networkSecurityGroup = New-AzureRmNetworkSecurityGroup @paramNetworkSecurityGroup  

Set the Virtual Network Subnet with the Network Security Group.

  1. # Set Virtual Network Subnet with Network security group  
  2. # Splat all parameters together for better readability. We are bundling all parameters  
  3. # for the command together to adhere to PowerShell coding best practices and have a better  
  4. # code readability  
  5. $paramNetworkSecurityGroupConfig =@{  
  6.   Name = "subnet-demo-accnetworking"  
  7.   VirtualNetwork=$vnet  
  8.   AddressPrefix = "10.8.1.0/24"  
  9.   NetworkSecurityGroup = $networkSecurityGroup  
  10. }  
  11. Set-AzureRmVirtualNetworkSubnetConfig @paramNetworkSecurityGroupConfig  

Create a dynamic IP address ip-demo-accnetworking. Create a Network Interface Card with Accelerated Networking enabled and assign the IP created to the Network Interface Card.

  1. # Create a dynamic IP  
  2. # Splat all parameters together for better readability. We are bundling all parameters  
  3. # for the command together to adhere to PowerShell coding best practices and have a better  
  4. # code readability  
  5. $paramIp = @{  
  6.   ResourceGroupName = "rg-demo-accnetworking"  
  7.   Name = "ip-demo-accnetworking"  
  8.   Location = "eastus"  
  9.   AllocationMethod = "Dynamic"  
  10. }  
  11. $ip = New-AzureRmPublicIpAddress @paramIp  
  12.  
  13. # Create a Network card with Accelerated Networking Enabled and in the same subnet created earlier  
  14. # with the created IP address  
  15. # Splat all parameters together for better readability. We are bundling all parameters  
  16. # for the command together to adhere to PowerShell coding best practices and have a better  
  17. # code readability  
  18. $paramNetworkCard = @{  
  19.   ResourceGroupName = "rg-demo-accnetworking"  
  20.   Name = "nic-demo-accnetworking"  
  21.   Location = "eastus"  
  22.   SubnetId = $vnet.Subnets[0].Id  
  23.   PublicIpAddressId = $ip.Id  
  24.   EnableAcceleratedNetworking = $true  
  25. }  
  26. $nic = New-AzureRmNetworkInterface @paramNetworkCard  

Create a Virtual Machine with required Operating System and VM size. Associate the Network Card created with Accelerated Networking enabled with the Virtual Machine. It is highly essential to create a Virtual Machine with supported Operated System and Virtual Machine size else Accelerated Networking will not get enabled.

  1. # Get credentials for Virtual Machines. It will prompt to key in user name and password    
  2. # to be set for the admin account of Virtual Machine    
  3. $cred = Get-Credential    
  4.   
  5. # Set size of Virtual Machine    
  6. $config = New-AzureRmVMConfig -VMName "vm-demo-accnt" -VMSize "Standard_DS4_v2"    
  7.   
  8. # Set Operating System of Virtual Machine    
  9. # Splat all parameters together for better readability. We are bundling all parameters    
  10. # for the command together to adhere to PowerShell coding best practices and have a better    
  11. # code readability    
  12. $paramVMConfig = @{    
  13.   VM = $config    
  14.   Windows = $true    
  15.   ComputerName = "vm-demo-accnt"    
  16.   Credential = $cred    
  17. }    
  18. $config = Set-AzureRmVMOperatingSystem @paramVMConfig    
  19.   
  20. # Set Operating System Image of the Virtual Machine    
  21. # Splat all parameters together for better readability. We are bundling all parameters    
  22. # for the command together to adhere to PowerShell coding best practices and have a better    
  23. # code readability    
  24.     
  25. $paramVMConfig = @{    
  26.   VM = $config    
  27.   PublisherName = "MicrosoftWindowsServer"    
  28.   Skus = "2016-Datacenter"    
  29.   Offer = "WindowsServer"    
  30.   Version = "Latest"    
  31. }    
  32.     
  33. $config = Set-AzureRmVMSourceImage @paramVMConfig    
  34.   
  35. # Associate Network Interface Card created with Accelerated Networking eanbled with the Virtual machine    
  36. $vmConfig = Add-AzureRmVMNetworkInterface -VM $config -Id $nic.Id    
  37. # Create Azure Virtual Machine    
  38. New-AzureRmVM -VM $vmConfig -ResourceGroupName "rg-demo-accnetworking" -Location "eastus"    

Wrap Up

The PowerShell code discussed is attached. As a prerequisite, PowerShell 5.1 along with Azure Powershell commandlets should be installed before executing this script. This script should be executed with Administrator permissions.