We had a requirement from one of the teams to deploy a virtual machine with multiple NIC associated with a single Virtual Machine.  The Azure portal does not provide any option to either associate a NIC ( Network Interface Card) to a Virtual Machine nor does it allow us to associate or create a Virtual Machine with more than one NIC.

It is always frustrating when you try to see how the Azure portal has been so well developed yet lacked these basic options which will make it more likable than ever.

After much discussion, we found this script useful for us to proceed further with our deployments for the client, hence sharing the same in case some one wants to find them easily.


#Description: Powershell script to deploy a virtual machine with multiple NIC cards in Azure
#Version: 1.0


# Pass Azure Credentials
$azureAccountNameEA ="<Your Azure Username>"
$azurePasswordEA = ConvertTo-SecureString "<Your Azure Password>" -AsPlainText –Force

# Create the Login object
$psCredEA = New-Object

# Set credentials
System.Management.Automation.PSCredential($azureAccountNameEA, $azurePasswordEA)

# Automatically login to Azure using above credentials
Login-AzureRmAccount -Credential $psCredEA

# Select subscription to work with
Select-AzureRMSubscription -subscriptionId "<subscription ID>"

$Location = ‘<region >’ #e.g. North Europe, East US, etc.
$RG = ‘<Resource Group Name>’ #Resource Group Name of the VM which will have multiple NICs 
$VNET = Get-AzureRmVirtualNetwork -Name ‘<VNet Name>’ -ResourceGroupName ‘<VNet Resource Group Name>’
$SubnetID = (Get-AzureRmVirtualNetworkSubnetConfig -Name ‘<Subnet Name>’ -VirtualNetwork $VNET).Id
$NICResourceGroup = ‘<Resource Group Name>’ #Resource Group Name where the NIC will be created, usually the same as the VM
$storageAcc = '<stoarge account>' # storage account for the VM
$VMName = ‘<VM Name>’ 


#NICs
$IPAddress1 = '<IP Address 1>'
$IPAddress2 = '<IP Address 2>'
$IPAddress3 = '<IP Address 3>'
$IPAddress3 = '<IP Address 4>'

$NICName1 = '<NIC Name 1>'
$NICName2 = '<NIC Name 2>'
$NICName3 = '<NIC Name 3>'
$NICName4 = '<NIC Name 4>'

#Create the NICs
New-AzureRmNetworkInterface -Name $NICName1 -ResourceGroupName $NICResourceGroup -Location $Location -SubnetId $SubnetID -PrivateIpAddress $IPAddress1
New-AzureRmNetworkInterface -Name $NICName2 -ResourceGroupName $NICResourceGroup -Location $Location -SubnetId $SubnetID -PrivateIpAddress $IPAddress2
New-AzureRmNetworkInterface -Name $NICName3 -ResourceGroupName $NICResourceGroup -Location $Location -SubnetId $SubnetID -PrivateIpAddress $IPAddress3
New-AzureRmNetworkInterface -Name $NICName4 -ResourceGroupName $NICResourceGroup -Location $Location -SubnetId $SubnetID -PrivateIpAddress $IPAddress4


#GET NICs
$NewNIC1 = Get-AzureRmNetworkInterface -Name $NICName1 -ResourceGroupName $NICResourceGroup
$NewNIC2 = Get-AzureRmNetworkInterface -Name $NICName2 -ResourceGroupName $NICResourceGroup
$NewNIC3 = Get-AzureRmNetworkInterface -Name $NICName3 -ResourceGroupName $NICResourceGroup
$NewNIC4 = Get-AzureRmNetworkInterface -Name $NICName4 -ResourceGroupName $NICResourceGroup

#Set VM credentials
#$cred = Get-Credential -Message "Type the name and password of the local administrator account."
$user = "<username>"
$password = '<Password>'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)


#Configure the VM Size, not all VMs support Multiple NIC, in this example we used Standard A4 which supports 4 NIC cards. Details can be found at https://docs.microsoft.com/en-gb/azure/virtual-machines/virtual-machines-windows-sizes

$vm = New-AzureRmVMConfig -VMName $VMName -VMSize "Standard_A4" 

#Define the latest image to be used from Azure Image Gallery, we have chosen the Windows Server 2012 R2 Datacenter edition
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest" 

#Set guest OS name 
$compName = "<Guest OS HostName>"
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $compName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate


#add NICs to the Network Interface Config
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NewNIC1.Id -Primary
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NewNIC2.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NewNIC3.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $NewNIC4.Id

# Show the Network interfaces
$vm.NetworkProfile.NetworkInterfaces



#Replace blob profile
$blobPath = "vhds/<vhdname>.vhd"

#$osDiskUri = $storageAcc + $blobPath
$storageAcc = Get-AzureRmStorageAccount '<storageaccount name' -ResourceGroupName $RG
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $blobPath

#Set the osDisk name and 
$diskName = "osdisk"
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage

#Create VM
New-AzureRmVM -ResourceGroupName $RG -Location $Location -VM $vm

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami