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. # Create Virtual Network along with subnet
  4. # Splat all parameters together for better readability. We are bundling all parameters
  5. # for the command together to adhere to PowerShell coding best practices and have a better
  6. # code readability
  7. $paramVNet = @{
  8. ResourceGroupName = "rg-demo-accnetworking"
  9. Location = "eastus"
  10. Name = "vnet-demo-accnetworking"
  11. AddressPrefix = "10.8.0.0/16"
  12. Subnet = $subnet
  13. }
  14. $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. # Create Network security group configuration with security rule configuration defined previously
  19. # Splat all parameters together for better readability. We are bundling all parameters
  20. # for the command together to adhere to PowerShell coding best practices and have a better
  21. # code readability
  22. $paramNetworkSecurityGroup =@{
  23. Name = "nsg-demo-accnetworking"
  24. ResourceGroupName="rg-demo-accnetworking"
  25. Location = "eastus"
  26. SecurityRules = $rdpConfig
  27. }
  28. $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. # Create a Network card with Accelerated Networking Enabled and in the same subnet created earlier
  13. # with the created IP address
  14. # Splat all parameters together for better readability. We are bundling all parameters
  15. # for the command together to adhere to PowerShell coding best practices and have a better
  16. # code readability
  17. $paramNetworkCard = @{
  18. ResourceGroupName = "rg-demo-accnetworking"
  19. Name = "nic-demo-accnetworking"
  20. Location = "eastus"
  21. SubnetId = $vnet.Subnets[0].Id
  22. PublicIpAddressId = $ip.Id
  23. EnableAcceleratedNetworking = $true
  24. }
  25. $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. # Set size of Virtual Machine
  5. $config = New-AzureRmVMConfig -VMName "vm-demo-accnt" -VMSize "Standard_DS4_v2"
  6. # Set Operating System of Virtual Machine
  7. # Splat all parameters together for better readability. We are bundling all parameters
  8. # for the command together to adhere to PowerShell coding best practices and have a better
  9. # code readability
  10. $paramVMConfig = @{
  11. VM = $config
  12. Windows = $true
  13. ComputerName = "vm-demo-accnt"
  14. Credential = $cred
  15. }
  16. $config = Set-AzureRmVMOperatingSystem @paramVMConfig
  17. # Set Operating System Image of the Virtual Machine
  18. # Splat all parameters together for better readability. We are bundling all parameters
  19. # for the command together to adhere to PowerShell coding best practices and have a better
  20. # code readability
  21. $paramVMConfig = @{
  22. VM = $config
  23. PublisherName = "MicrosoftWindowsServer"
  24. Skus = "2016-Datacenter"
  25. Offer = "WindowsServer"
  26. Version = "Latest"
  27. }
  28. $config = Set-AzureRmVMSourceImage @paramVMConfig
  29. # Associate Network Interface Card created with Accelerated Networking eanbled with the Virtual machine
  30. $vmConfig = Add-AzureRmVMNetworkInterface -VM $config -Id $nic.Id
  31. # Create Azure Virtual Machine
  32. 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.