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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Recently tested fully with a later version of the Azure PowerShell module – Install-Module AzureRM -RequiredVersion 4.4.1 | |
#region Logon to Azure | |
Login–AzureRmAccount | |
$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 |