Add additional NIC to Azure ARM VM

Sometimes you would want to add an additional Network Interface Card to a Microsoft Azure ARM (Azure Resource Manager) based virtual machine.

PowerShell is your answer:


# Recently tested fully with a later version of the Azure PowerShell module – Install-Module AzureRM -RequiredVersion 4.4.1
#region Logon to Azure
LoginAzureRmAccount
$subscription = Get-AzureRmSubscription | Out-GridView PassThru
Select-AzureRmSubscription SubscriptionId $subscription.Id
#endregion
# Variables for YOu to fill in
$ResourceGroup = 'MyResourceGroupName' # resource group name to contain the new NIC
$VMname = 'VMware-01' # VM you want to add the new NIC to
$NICName = 'VMware-NIC' # for the new NIC
$Location = 'southeastasia' # for the new NIC
# Get the VNET to which to connect the NIC
$VNET = Get-AzureRmVirtualNetwork Name 'NestedVMware-vnet' ResourceGroupName $ResourceGroup
# Get the Subnet ID to which to connect the NIC
$SubnetID = (Get-AzureRmVirtualNetworkSubnetConfig Name 'default' VirtualNetwork $VNET).Id
#–> Create now the NIC Interface
New-AzureRmNetworkInterface Name $NICName ResourceGroupName $ResourceGroup Location $Location SubnetId $SubnetID
#Get the VM config to a variable
$VM = Get-AzureRmVM Name $VMname ResourceGroupName $ResourceGroup
#Stop the VM
Stop-AzureRmVM Name $VM.Name ResourceGroupName $VM.ResourceGroupName StayProvisioned Force
#Add the second NIC to the VM variable
$NewNIC = Get-AzureRmNetworkInterface Name $NICName ResourceGroupName $ResourceGroup
$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 $ResourceGroup

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s