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.
- # Log In to Azure
- LogIn-AzureRMAccount
Now, let us create a Resource Manager. Below PowerShell code will create a Resource manager named rg-demo-accnetworking in location eastus.
- # Create Resource Group
- 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.
- # Create subnet configuration
- $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name "subnet-demo-accnetworking" -AddressPrefix "10.8.1.0/24"
-
- # Create Virtual Network along with subnet
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramVNet = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Location = "eastus"
- Name = "vnet-demo-accnetworking"
- AddressPrefix = "10.8.0.0/16"
- Subnet = $subnet
- }
- $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.
- # Create Network security rule configuration that will allow RDP connection
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroupRuleConfig = @{
- Name = "rg-demo-nsg-allow-rdp"
- Description = "Enable RDP connection"
- Access = "Allow"
- Protocol = "TCP"
- Direction = "Inbound"
- Priority = 100
- SourceAddressPrefix = "*"
- SourcePortRange = "*"
- DestinationAddressPrefix = "*"
- DestinationPortRange = 3389
- }
- $rdpConfig = New-AzureRmNetworkSecurityRuleConfig @paramNetworkSecurityGroupRuleConfig
-
- # Create Network security group configuration with security rule configuration defined previously
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroup =@{
- Name = "nsg-demo-accnetworking"
- ResourceGroupName="rg-demo-accnetworking"
- Location = "eastus"
- SecurityRules = $rdpConfig
- }
- $networkSecurityGroup = New-AzureRmNetworkSecurityGroup @paramNetworkSecurityGroup
Set the Virtual Network Subnet with the Network Security Group.
- # Set Virtual Network Subnet with Network security group
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroupConfig =@{
- Name = "subnet-demo-accnetworking"
- VirtualNetwork=$vnet
- AddressPrefix = "10.8.1.0/24"
- NetworkSecurityGroup = $networkSecurityGroup
- }
- 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.
- # Create a dynamic IP
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramIp = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Name = "ip-demo-accnetworking"
- Location = "eastus"
- AllocationMethod = "Dynamic"
- }
- $ip = New-AzureRmPublicIpAddress @paramIp
-
- # Create a Network card with Accelerated Networking Enabled and in the same subnet created earlier
- # with the created IP address
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkCard = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Name = "nic-demo-accnetworking"
- Location = "eastus"
- SubnetId = $vnet.Subnets[0].Id
- PublicIpAddressId = $ip.Id
- EnableAcceleratedNetworking = $true
- }
- $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.
- # Get credentials for Virtual Machines. It will prompt to key in user name and password
- # to be set for the admin account of Virtual Machine
- $cred = Get-Credential
-
- # Set size of Virtual Machine
- $config = New-AzureRmVMConfig -VMName "vm-demo-accnt" -VMSize "Standard_DS4_v2"
-
- # Set Operating System of Virtual Machine
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramVMConfig = @{
- VM = $config
- Windows = $true
- ComputerName = "vm-demo-accnt"
- Credential = $cred
- }
- $config = Set-AzureRmVMOperatingSystem @paramVMConfig
-
- # Set Operating System Image of the Virtual Machine
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
-
- $paramVMConfig = @{
- VM = $config
- PublisherName = "MicrosoftWindowsServer"
- Skus = "2016-Datacenter"
- Offer = "WindowsServer"
- Version = "Latest"
- }
-
- $config = Set-AzureRmVMSourceImage @paramVMConfig
-
- # Associate Network Interface Card created with Accelerated Networking eanbled with the Virtual machine
- $vmConfig = Add-AzureRmVMNetworkInterface -VM $config -Id $nic.Id
- # Create Azure Virtual Machine
- 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.