Enable Azure Accelerated Networking

Here’s a script to enable Azure Accelerated Networking for existing Azure Virtual Desktops VMs.

Script will check all VMs in the subscription to see if they are deallocated and if Accelerated Networking is enabled. If the VM is deallocated and accelerated networking is not enabled then the script will enable it.

Uncomment these lines when ready to make changes.
#$nic.EnableAcceleratedNetworking = $true
#$nic | Set-AzNetworkInterface
This script below can be used for one off VMs. If you want to enable it for multiple then see below.
# Used for a Single VM.
# Parameters
$resourceGroupName = "RG"
$vmName = "AVD"

# Get the VM
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName

# Check if VM is deallocated
$vmStatus = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -Status
if ($vmStatus.Statuses[1].Code -ne 'PowerState/deallocated') {
    Write-Output "The VM must be deallocated before enabling Accelerated Networking. Please deallocate the VM and try again."
    exit
}

# Get NIC associated with the VM
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id

# Enable Accelerated Networking
#$nic.EnableAcceleratedNetworking = $true
#$nic | Set-AzNetworkInterface

Write-Output "Accelerated Networking has been enabled on $vmName."

This script can be used if you smaller amount of VMs. If you have 100+ then you will probably want use the script at the bottom that uses a resource graph query.

# Get all VMs in the subscription
$allVMs = Get-AzVM

foreach ($vm in $allVMs) {
    # Get NIC associated with the VM
    $nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
    
    # Display NIC details
    Write-Output "Processing VM $($vm.Name) in Resource Group $($vm.ResourceGroupName) with NIC named $($nic.Name) having ID $($nic.Id)."

    # Check if Accelerated Networking is already enabled
    if ($nic.EnableAcceleratedNetworking -eq $true) {
        Write-Output "Accelerated Networking is already enabled on $($vm.Name) in Resource Group $($vm.ResourceGroupName). Skipping..."
        continue
    }

    # Check if VM is deallocated
    $vmStatus = Get-AzVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status
    if ($vmStatus.Statuses[1].Code -ne 'PowerState/deallocated') {
        Write-Output "The VM $($vm.Name) in Resource Group $($vm.ResourceGroupName) must be deallocated before enabling Accelerated Networking. Please deallocate the VM and try again."
        continue
    }

    # Enable Accelerated Networking
    #$nic.EnableAcceleratedNetworking = $true
    #$nic | Set-AzNetworkInterface
	
	Write-Output "Accelerated Networking has been enabled on $($vm.Name) in Resource Group $($vm.ResourceGroupName)."
}
<#
.SYNOPSIS
This script retrieves deallocated Virtual Machines (VMs) using Azure Resource Graph and checks/enables Accelerated Networking (AN) on their network interfaces (NICs).
 
.DESCRIPTION
1. The script uses the 'Search-AzGraph' cmdlet to find all deallocated VMs by querying the Azure Resource Graph. The specific query is defined in the `$query` variable.
2. It implements pagination logic to handle the retrieval of large numbers of VMs, making successive calls to 'Search-AzGraph' to retrieve all VMs in batches of size defined in `$first`.
3. For each deallocated VM retrieved, it finds the associated NIC, checks whether AN is enabled, and if it is not, enables it. Note that the NIC enabling is commented out.
4. It outputs the VMs for which AN was not enabled in a table format.

.PARAMETERS
- `$first`: Number of VMs to retrieve in each batch. Defaults to 1000.
- `$query`: A query to find deallocated VMs and project the necessary properties.

.NOTES
- Ensure Azure PowerShell and Az.ResourceGraph modules are installed and that you're authenticated to Azure before running.
- Validate and test thoroughly in a safe environment before running in production.

.AUTHOR
[Your Name]

#>

# Parameters
$first = 1000  # First 1000 VMs

# Query: Adjusted for simplicity
$query = @"
Resources
| where type =~ 'microsoft.compute/virtualmachines'
| where properties.extended.instanceView.powerState.code == 'PowerState/deallocated'
| project vmName=name, resourceGroup=resourceGroup, nicId=properties.networkProfile.networkInterfaces[0].id
"@

$allDeallocatedVMs = @()
$skip = $null

do {
    if($null -eq $skip) {
        $deallocatedVMs = Search-AzGraph -Query $query -First $first
    } else {
        $deallocatedVMs = Search-AzGraph -Query $query -First $first -Skip $skip
    }
    
    if($deallocatedVMs -eq $null -or $deallocatedVMs.Count -eq 0){
        Write-Output "No more VMs found."
        break
    }

    $allDeallocatedVMs += $deallocatedVMs
    Write-Output "Retrieved $($allDeallocatedVMs.Count) VMs so far..."
    
    $skip = $allDeallocatedVMs.Count
} while ($true)

Write-Output "Retrieved a total of $($allDeallocatedVMs.Count) VMs."

# Processing
$vmsWithoutAN = @()

foreach ($vm in $allDeallocatedVMs) {
    $nicId = $vm.nicId
    $nicName = ($nicId -split '/')[-1]
    $rgName = $vm.resourceGroup
    
    $nic = Get-AzNetworkInterface -ResourceGroupName $rgName -Name $nicName
    
    if (-not $nic.EnableAcceleratedNetworking) {
        $vmsWithoutAN += $vm
        Write-Output "Enabling Accelerated Networking on NIC: $nicName for VM: $($vm.vmName) in Resource Group: $rgName..."
        #$nic.EnableAcceleratedNetworking = $true
        #Set-AzNetworkInterface -NetworkInterface $nic
    }
}

# Output VMs that were without AN
$vmsWithoutAN | Format-Table vmName, resourceGroup
Share or Save this:
Share