/ AZURE, SCRIPTS

Enable Accelerated Networking for All of an Azure VMs NICs

The accelerated networking feature is a great way to give network performance of your Azure VMs a quick boost.

Lately we’d had issues with an application heavily leveraging SMB having an impacted performance on Azure, but working fine in an identical on-prem environment.

We found that after enabling the Accelerated Networking feature on the NICS for the application server and for each VDI VM the latency was much lower, and application experience was much higher.

Below is a script which will easily switch this feature on for all of a VM’s NICs - saving you the headache of running around and getting each NIC name, ID and RG.

Please note that to enable this feature the VMs must be deallocated, so please do not run this without scheduling an outage window.

Write-Host "IMPORTANT : Please understand that running this script will temporarily shut down and deallocate the targetted VM while enabling Accelerated Networking" -ForegroundColor Yellow

Login-AzureRmAccount
$vmName = Read-Host "Enter the name of the VM you wish to change"

$resourceGroups = Get-AzureRmResourceGroup
$VMResourceGroup

#Check Each RG for the VM
ForEach($RG in $resourceGroups){
    If($vmObject -eq $null){
        $vmObject = Get-AzureRmVM -Name $vmName -ResourceGroupName $rg.ResourceGroupName -ErrorAction SilentlyContinue 
    }
}

# VM Confirm  - confirm if it's correct or advise none found
If($vmObject -ne $null){ 
    Write-Host "Found VM matching name $($vmName) in resource group $($vmObject.ResourceGroupName)" -ForegroundColor Cyan
    Write-host "Please enter 'y' to confirm the target VM or 'n' to abort:" -ForegroundColor Yellow
    $confirm = Read-Host

}Else{
    Write-Host "Could not find a VM matching name $($vmName) in all $($resourceGroups.Count) resource groups"
}

#After confirmation we now run the bulk of the script
If($confirm -eq "y"){
    #First we deallocate the VM
    Write-Host "Trying to deallocate $($vmObject.Name) now" -ForegroundColor Cyan
    Stop-AzureRmVM -Name $vmObject.Name -ResourceGroupName $vmObject.ResourceGroupName -Force

    #Get all NICs that are attached, and where the VM ID matches our targetted VM 
    $VMNics = Get-AzureRMNetworkInterface | Where-Object {$_.VirtualMachine -ne $null -and $_.VirtualMachine.Id -eq "$($vmObject.ID)"}
    Write-Host "$($vmObject.Name) has $($VMNics.Count) NICs" -ForegroundColor Cyan

    #For Each NIC we discovered set AN to be true
    ForEach($NIC in $VMNICS){
        Write-Host "Enabling accelerated networking for NIC $($NIC.Name) in RG $($NIC.ResourceGroupName)" -ForegroundColor Yellow
        $nic.EnableAcceleratedNetworking = $true
        $nic | Set-AzureRmNetworkInterface 
        Write-Host "Enabled for NIC $($NIC.Name)" -ForegroundColor Green
    }

    #Done updating NICs, power on VM if desired
    Write-Host "Would you like to turn the VM back online?" -ForegroundColor Yellow
    $powerOnConfirm = Read-Host "Please enter 'y' to confirm or 'n' to cancel"
    If($powerOnConfirm -eq "y"){
        Start-AzureRMVM -Name $vmObject.Name -ResourceGroupName $vmObject.ResourceGroupName
    }  

    Write-Host "All Done" -ForegroundColor Green

}Else{ # If the user did not confirm earlier the script will exit as below
    Write-Host "Aborting task, press enter to exit..." -ForegroundColor Yellow
    Read-Host
}

Download Script (PS1 format)