This is one I use regularly, so thought it might come in handy for you..

Assumption here is you are connected via PowerShell to your intended subscription..

 

A few key points:

  • The VM will need to be stopped to execute
  • Check the Azure VM sizing to see how many NICs you can attach
  • I have a pre-created Network Interface with my desired settings
#Using OGV to target your objects
$RG = Get-AzureRmResourceGroup | ogv -PassThru
$NIC = Get-AzureRmNetworkInterface | ogv -PassThru
$VM = Get-AzureRmVm | ogv -PassThru

#The actual adding of the NIC to the VM
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC.Id

# Show the Network interfaces (just for clarity)
$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 $RG.ResourceGroupName

# Fire up your new config
Start-AzureRmVM -VM $VM -ResourceGroupName $RG.ResourceGroupName

Enjoy! Dan

Daniel Apps

Hi, I'm Daniel Apps — AI platform enthusiast, unapologetic infrastructure nerd, and dad to two small humans. I write about infrastructure, AI industry topics, and the real-world chaos of modern IT.

More about me →

s