How to add a NIC to an Azure Virtual Machine (ARM)

Hi all,

In this short post, I will show you how to add a  NIC (third, fourth…) to an Azure virtual machine under Azure Resource Manager and using Azure Powershell.

Before continuing, keep in mind:

– Not all the virtual machines sizes support multiple NICs. Check if your VM size is supported ( https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-size-specs/)

– The VM should already have at least two NICs. It’s not supported to pass from a single NIC VM to multiple NIC VM and vice versa

If you are creating the VM with multiple NICs (Use this post to do it), the resource group to which you are deploying the VM must contain only [multiple NIC]’s VM types. In other words, you cannot create a VM with multiple NICs in a resource group containing [single NIC]’s VMs It seems that this limitation was removed

– The VM will be rebooted when adding a virtual NIC

– You can use Azure CLI, Azure API or Powershell to make this operation. The portal does not provide a way to add a  NIC.

– In this post I’m using Azure Powershell 1.0. If you are using the 0.9.8 or prior, remove the ‘Rm’ suffix from your commands and change the mode to ResourceManager

Walkthrough

This is easy, only two steps:

  1. Create a new Virtual NIC
  2. Attach the VNIC to the virtual machine

1- Create a new Virtual NIC

The first step is to create a Virtual NIC to associate it later to to the virtual machine. You will need to provide the following information:

  • The  Name of the virtual NIC to be created : $NICName
  • The Resource Group Name where the VNIC will be created : $NICResourceGroup
  • The location/region where the VNIC will be created, of course it has to match the VNET/Subnet location : $Location
  • The subnet ID to which you will connect the VNIC, you can get it via Powershell (See my example below) : $SubnetID
  • The IP address you want to assign to the NIC : $IPAddress

Powershell

# Get the VNET to which to connect the NIC
$VNET = Get-AzureRmVirtualNetwork -Name ‘VnetV2’ -ResourceGroupName ‘RGNetworkV2’
# Get the Subnet ID to which to connect the NIC
$SubnetID = (Get-AzureRmVirtualNetworkSubnetConfig -Name ‘VNETV2Subnet01’ -VirtualNetwork $VNET).Id
# NIC Name
$NICName = ‘A3VM-NIC3’
#NIC Resource Group
$NICResourceGroup = ‘RG’
#NIC creation location
$Location = ‘north europe’
#Enter the IP address
$IPAddress = ‘10.20.30.21’

#–> Create now the NIC Interface

New-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $NICResourceGroup -Location $Location -SubnetId $SubnetID -PrivateIpAddress $IPAddress

 

2-  Attach the VNIC to the Virtual Machine

The second and last step is to associate the created VNIC to to the virtual machine. You will need to provide the following information:

  • The virtual machine Name : $VMname
  • The Virtual machine resource Group : $VMRG

Powershell

$VMname = ‘VMname’
$VMRG =  ‘VM-RG’

#Get the VM
$VM = Get-AzureRmVM -Name $VMname -ResourceGroupName $VMRG

#Add the second NIC
$NewNIC =  Get-AzureRmNetworkInterface -Name $NICName -ResourceGroupName $NICResourceGroup
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NewNIC.Id

# Show the Network interfaces
$VM.NetworkProfile.NetworkInterfaces

#we have to set one of the NICs to Primary, i will set the first NIC in this example
$VM.NetworkProfile.NetworkInterfaces.Item(0).Primary = $true

#Update the VM configuration (The VM will be restarted)
Update-AzureRmVM -VM $VM -ResourceGroupName $VMRG

Advertisements

24 thoughts on “How to add a NIC to an Azure Virtual Machine (ARM)

  1. Thanks for this… its very helpful. I have an azure vm which I need to add a second NIC to. After changing the size to allow for this option I followed the instructions above. However, I’ve received the following error:

    Update-AzureRmVM : Virtual machines with multiple network interfaces and virtual machines with a single network interface are not supported in the same availability set, also a virtual machine having a single network interface cannot be updated to have multiple network interfaces and vice-versa.
    StatusCode: 400
    ReasonPhrase: Bad Request
    OperationID : ‘e2ae83d8-e61c-4de6-b292-f838ddea1b7b’
    At line:1 char:1
    + Update-AzureRmVM -VM $VM -ResourceGroupName $VMRG

    I take it my setup doesn’t support this configuration?

    • Hi,
      As i mentioned in my first comment, the VM should already have at least two NICs when created. Your scenario is not supported. You can use my last blog to create a multi-nic VM but you should change the OS disk config to use an existing disk. If you don’t success to do it, leave a comment and i will update it with such configuration

  2. Update-AzureRmVM : MultipleAndSingleNetworkInterfaceVirtualMachinesNotSupported: Virtual machines
    with multiple network interfaces and virtual machines with a single network interface are not
    supported in the same availability set, also a virtual machine having a single network interface
    cannot be updated to have multiple network interfaces and vice-versa.
    OperationID : ‘8d7a2c10-9b20-42aa-a001-efcf87d427be’
    At line:1 char:1
    + Update-AzureRmVM -VM $VM -ResourceGroupName $VMRG
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Update-AzureRmVM], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.UpdateAzureVMCommand

  3. Hi Samir,

    Everything seems to work ok for me, but when I log into the Windows VM via RDP, I can only ever see the original two NICs that were there when I deployed the VM. In the Azure Portal, I can see 3 NICs, but Windows never recognizes the third NIC. Any thoughts?

    Thank you!

  4. Sorry, super new at this. I followed your example and was able to add a second NIC to my F8 VM and when I print out $VM, it shows it as NetworkInterface[1] but the Azure web interface doesnt show it as attached to my VM. Any ideas?

  5. Thanks for posting this awesome article. I search since a long time an answer to
    this subject and I have finally found it on your site.
    I saved your blog in my rss feed and shared it on my Twitter.
    Thanks again for this great article!

  6. Hello,
    if i understood, If i have an existing vm with 1 nic (but the size of vm have the possibility to add nic) i have to redeploy it?
    Thanks

  7. Great Article Samir, but I am having problems when I try to delete the first nic. It says that may virtual machine is using it. Any Ideas or maybe you have an article to solve this ?

      • Thanks, Samir for your reply, the thing is, that I wanted to rename the NiC, but I couldn’t find any method to accomplish this. So I created an additional NIC and configured it as the primary NIC. But after this there is no way to remove the first nic. The error I am getting is “unable to remove the nic….. beause its being used by xxxx virtual machine.
        Thank you if you have solution for this.

  8. Thanks for replying to me the first time. I opened a case with Microsoft and this is what they informed me:

    Subject: RE: [REG:] AZURE VM|SEV B|ISSUES TO ERASE NETWORK INTERFACE|WINDOWS SERVER 2016 VM| Respuesta inicial

    Hi Luis,
    Our online documentation had a couple of errors:
    https://docs.microsoft.com/en-us/azure/virtual-machines/windows/multiple-nics#remove-a-nic-from-an-existing-vm
    ________________________________________
    WRONG
    $vm.NetworkProfile.NetworkInterfaces
    Remove-AzureRmNetworkInterface -Name “myNic3” -ResourceGroupName “myResourceGroup” | `
    Update-AzureRmVm -ResourceGroupName “myResourceGroup”
    ________________________________________
    EXPLANATION
    AzureRmNetworkInterface – we’re not trying to remove the NIC from the subscription, just the VM (for now)
    “myNic3″ – This is expecting the actual NIC, not a string value.

    ________________________________________
    CORRECT CMDLET (in yellow)
    You need to edit these parameters (in green) to suit your environment.
    $rg=”MultiNicRG”
    $NicToKill = “myNic3”
    $vmName = “myVM”

    # Assign the NIC and then view assigned NICs. Notice which is Primary=True.
    $myNic = Get-AzureRmNetworkInterface -Name $NicToKill -ResourceGroupName $rg
    $vm = Get-AzureRmVm -Name $vmName -ResourceGroupName $rg
    $vm.NetworkProfile.NetworkInterfaces

    Remove-AzureRmVMNetworkInterface -VM $vm -NetworkInterfaceIDs $myNic.Id

    # Update the VM state in Azure
    Update-AzureRmVM -VM $vm -ResourceGroupName $rg
    # Assign the NIC and then view assigned NICs. Notice which is Primary=True.
    $vm = Get-AzureRmVm -Name $vmName -ResourceGroupName $rg
    $vm.NetworkProfile.NetworkInterfaces

    So you know that this a problem, The PS commands supplied by MS worked !!!.

  9. Bye the way. The above mentioned post will only remove the Nic association with the VM, it does not delete it from the RG.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s