Add a Network Interface to an Azure VM
In today’s Ask the Admin, I’ll show you how to add a network interface to an Azure virtual machine.
In Configure Multiple NICs in an Azure Virtual Machine on the Petri IT Knowledgebase, I showed you how to provision a virtual machine (VM) with two or more network interfaces (NICs) from the get go. But what if you want to add additional NICs at a later stage? That’s what I’m going to show you how to do today.
Read the Best Personal and Business Tech without Ads
Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.
While it’s possible to create NICs in the Azure management portal, it’s not possible to attach them to VMs. That can only be done using PowerShell. So, for the purposes of this article, it makes sense that we do everything in PowerShell. Not all Azure VM sizes support multiple NICs, so make sure you check the size of your VM before trying to add an additional NIC. For more information on VM sizes, see Microsoft’s website here.
Before following the instructions below, you’ll need to have an active Azure subscription. If you don’t already have one, you can sign up for a free trial here. Also, make sure you have the latest version of Microsoft Azure PowerShell installed on your PC. The latest release can be installed using the Web Platform Installer.
Additionally, your VM must already have multiple NICs. A VM with a single NIC cannot be converted into a VM with multiple NICs. That is, if your VM already has two NICs, you can add a third using the method that follows. But if your VM has only one NIC, you cannot add a second. The commands apply to VMs and resources created using Azure Resource Manager and cannot be used for classic VMs.
Create a New NIC and Private IP Address
The first step is to open Windows PowerShell ISE and log in to your Azure subscription using the Login-AzureRmAccount cmdlet. Let’s define some variables for our VM, network, and other parameters that will be required.
$RG defines the Resource Group where the VM and VNET are located. The new NIC will be created in this Resource Group. I picked a name for the new NIC ($nicName), and a private IP address, which I know is available ($ipAddress). If you are not sure what the rest of the values should be for your deployment, locate the VM in the Azure management portal, and you’ll find information about the deployment, network, and any other related resources.
$vmName = ‘myVM’ $vnetName = ‘myVNET’ $RG = ‘PetriRG’ $subnetName = ‘Subnet-1’ $nicName = ‘PetriVM-NIC3’ $location = ‘North Europe’ $ipAddress = ’10.0.0.6’
The next step is to create an object for the VM using Get-AzureRmVM:
$myVm = Get-AzureRmVM -Name $vmName -ResourceGroupName $RG
And now we’ll create an object for the virtual network using Get-AzureRmVirtualNetwork:
$vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $RG
The new NIC needs to be connected to a subnet ($subnetName), but we need to know the subnet ID before we can work with it in PowerShell:
$subnetID = (Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet).Id
Finally, we can create a new NIC using the New-AzureRmNetworkInterface cmdlet:
New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $RG -Location $location -SubnetId $subnetID -PrivateIpAddress $ipAddress
Attach the NIC to a VM
Now that a new NIC has been created, all that’s left to do is to attach it to the VM. First, we’ll create an object for the NIC using the Get-AzureRmNetworkInterface cmdlet:
$myNIC = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $RG
Let’s add the NIC to the VM configuration:
$myVm = Add-AzureRmVMNetworkInterface -VM $myVm -Id $myNIC.Id
If you want to check that the configuration has been changed to include the new NIC, you can run the command below to see the list of NICs attached to the VM:
Note that the primary NIC (0) is the first NIC in the list. If you want to set another NIC as the primary NIC, for instance, the third NIC (2), run the command below:
$myVM.NetworkProfile.NetworkInterfaces.Item(2).Primary = $true
Lastly, let’s commit the new configuration using the Update-AzureRmVM cmdlet:
Update-AzureRmVM -VM $myVm -ResourceGroupName $RG
In this article, I showed you how to add a NIC to an Azure VM.