Contents of this Article
Introduction
Renaming the NIC interface for an Azure VM is not a new problem. If you find yourself having to do this, you know how painful it is. This is the case when you deploy a Virtual Machine using the Azure Portal, however, if you deploy the VM using Azure PowerShell, ARM template, or Azure CLI, then you can specify the NIC interface name at deployment time.
Once the VM is deployed using the portal, Azure will end up creating a random name for the Network interface as showing in the following screenshot:
If you are following a standard naming convention in your company, then you want to rename the NIC interface by following the steps below as documented by Microsoft:
- Stop (de-allocate) the VM.
- Dissociate the public IP address from the NIC interface (if exists).
- Create a new network adapter interface.
- Assign the new NIC interface to the VM.
- Remove the old NIC interface from the VM.
- Preserve the old NIC settings and apply them to the VM (Private IP address, Network Security Group, Public IP, etc.).
- Delete the old NIC interface resource.
- Start the VM.
In this blog post, I will share with you how to automate and rename the NIC interface for an Azure VM with PowerShell. The same can apply whether you are using a Linux or Windows VM.
If you are looking to rename the OS Disk for an Azure virtual machine, please check my previous article here.
Rename Azure VM NIC Interface
You have a couple of options to run the script, you can either use Azure Cloud Shell, Visual Studio Code, or the new Windows Terminal. The Script works with PowerShell 5.1 or PowerShell 7 (core) with Az module.
.EXAMPLE
.\Rename-AzVMNIC.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -NewNicName [NewNicName] -Verbose
Note: The script has not been tested when the VM is a member of an Availability Set. Your feedback is highly appreciated!
PowerShell Code
The complete script is detailed below to automate the entire process:
<# .SYNOPSIS Rename Azure VM NIC. .DESCRIPTION Rename Azure Network Adapter interface for Linux and Windows VM. .NOTES File Name : Rename-AzVMNIC.ps1 Author : Microsoft MVP - Charbel Nemnom Version : 1.0 Date : 22-September-2019 Update : 25-September-2019 Requires : PowerShell 5.1 or PowerShell 7 (Core) Module : Az Module OS : Windows or Linux VMs .LINK To provide feedback or for further assistance please visit: https://charbelnemnom.com .EXAMPLE .\Rename-AzVMNIC.ps1 -resourceGroup [ResourceGroupName] -VMName [VMName] -NewNicName [NewNicName] -Verbose This example will rename the NIC interface for the specified VM, you need to specify the Resource Group name, VM name and the new NIC name. The script will preserve the old network settings and apply them to the new network interface. #> [CmdletBinding()] Param ( [Parameter(Position = 0, Mandatory = $true, HelpMessage = 'Enter the Resource Group of the VM')] [Alias('rg')] [String]$resourceGroup, [Parameter(Position = 1, Mandatory = $True, HelpMessage = 'Enter Azure VM name')] [Alias('VM')] [String]$VMName, [Parameter(Position = 2, Mandatory = $true, HelpMessage = 'Enter the desired NIC interface name')] [Alias('NewNIC')] [String]$NewNicName ) #! Check Azure Connection Try { Write-Verbose "Connecting to Azure Cloud..." Connect-AzAccount -ErrorAction Stop | Out-Null } Catch { Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!" Break } #! Get the details of the VM Write-Verbose "Get the VM information details: $VMName" $VM = Get-AzVM -Name $VMName -ResourceGroupName $resourceGroup #! Get the virtual NIC interface name and details Write-Verbose "Get the old virtual NIC interface name and details..." $oldNicName = $VM.NetworkProfile.NetworkInterfaces.Id.Split('/')[-1] $vNic = Get-AzNetworkInterface -Name $oldNicName -ResourceGroupName $resourceGroup #! Get the public ip address of the virtual machine if exists. If ($VNic.IpConfigurations.publicIPAddress.Id -ne $null) { $PIpName = $VNic.IpConfigurations.publicIPAddress.Id.Split('/')[-1] $vNic.IpConfigurations.publicipaddress.id = $null Write-Verbose "Dissociate the public IP address from the VM: $PIpName" Set-AzNetworkInterface -NetworkInterface $vnic | Out-Null } #! Stop the VMName Write-Verbose "Stop and deallocate the VM: $VMName, please wait..." Stop-AzVM -Name $VMName -ResourceGroupName $resourceGroup -Force -Confirm:$false | Out-Null #! Create the new virtual NIC interface Write-Verbose "Creating the new virtual Network interface..." $NIC = New-AzNetworkInterface -Name $NewNicName -ResourceGroupName $resourceGroup ` -Location $VM.Location -SubnetId $vnic.IpConfigurations.Subnet.Id ` -IpConfigurationName $vnic.IpConfigurations.Name #! Remove the old NIC interface from the VM Write-Warning "Removing the old NIC interface: $($oldNicName) from the VM: $VMName" Remove-AzVMNetworkInterface -vm $VM -NetworkInterfaceIDs $vNic.Id | Out-Null #! Add the new NIC interface Write-Verbose "Adding the new network adapter interface to the VM..." Add-AzVMNetworkInterface -VM $VM -NetworkInterface $NIC | Update-AzVM -ResourceGroupName $resourceGroup | Out-Null #! Delete the old NIC interface resource Write-Warning "Deleting the old NIC interface: $($oldNicName)" Remove-AzNetworkInterface -Name $oldNicName -ResourceGroupName $vNic.ResourceGroupName -Force -Confirm:$false #! Update the new virtual NIC settings $NIC = Get-AzNetworkInterface -Name $NewNicName -ResourceGroupName $resourceGroup If ($vNic.Tag -ne $null) { $NIC = $vNIC.Tags } $NIC.DnsSettings = $vNIC.DnsSettings $NIC.EnableIPForwarding = $vNIC.EnableIPForwarding $NIC.EnableAcceleratedNetworking = $vNIC.EnableAcceleratedNetworking $NIC.NetworkSecurityGroup = $vNIC.NetworkSecurityGroup #! Set the new virtual NIC settings Write-Verbose "Set the new NIC interface settings..." If ($NIC.IpConfigurations.PrivateIpAddress -ne $vNIC.IpConfigurations.PrivateIpAddress) { Set-AzNetworkInterfaceIpConfig -NetworkInterface $NIC -Name $NIC.IpConfigurations.Name ` -PrivateIpAddressVersion $vNIC.IpConfigurations.PrivateIpAddressVersion ` -PrivateIpAddress $vNIC.IpConfigurations.PrivateIpAddress -SubnetId $vnic.IpConfigurations.Subnet.id | Out-Null } Set-AzNetworkInterface -NetworkInterface $NIC | Out-Null #! Create a public IP for the VM. If ($PIpName) { $PublicIp = Get-AzPublicIpAddress -Name $PIpName -ResourceGroupName $resourceGroup $NIC | Set-AzNetworkInterfaceIpConfig -Name $NIC.IpConfigurations.Name -PublicIPAddress $PublicIp ` -Subnet $vnic.IpConfigurations.Subnet | Out-Null Write-Verbose "Associate the public IP address to the VM: $PIpName" Set-AzNetworkInterface -NetworkInterface $NIC | Out-Null } #! Start the VMName Write-Verbose "Start the VM: $VMName, please wait..." Start-AzVM -Name $VMName -ResourceGroupName $resourceGroup -Confirm:$false | Out-Null
If you look back in the Azure Portal, we can see that the new NIC interface name is attached successfully and the previous settings are preserved.
If you have any feedback or changes that everyone should receive, please feel free to leave a comment below.
That’s it there you have it. Happy Azure Networking!
__
Thank you for reading my blog.
If you have any questions or feedback, please leave a comment.
-Charbel Nemnom-