Last active
November 25, 2018 09:46
-
-
Save jayankandathil/3d2758c69f7bcd2ed3dfc547ea4350ed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# https://docs.microsoft.com/en-us/azure/virtual-machines/windows/multiple-nics | |
# "Stop" the VM first | |
$subscriptionname = "yoursubscription" | |
$resourcegroupname = "yourresourcegroup" | |
$vmname = "yourvmname" | |
$vnetname = "vnetname" | |
$subnetname = "subnetname" | |
$wantednicname = "newnicname" | |
$unwantednicname = "oldnicname" | |
# --------------- | |
# AUTHENTICATION | |
# --------------- | |
Login-AzureRmAccount | |
Select-AzureRmSubscription -SubscriptionName $subscriptionname | |
$vm = Get-AzureRmVm -Name $vmname -ResourceGroupName $resourcegroupname | |
# Get info for the back end subnet | |
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $resourcegroupname | |
$subnet = $vnet.Subnets|?{$_.Name -eq '$subnetname'} | |
# Get the ID of the new virtual NIC and add to VM | |
$nic = (Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroupname -Name $wantednicname).Id | |
# List existing NICs on the VM and find which one is primary | |
$vm.NetworkProfile.NetworkInterfaces | |
# Add second NIC (expect an eror here - "Virtual machine Jayan-AEMReporting-Publish1 must have one network interface set as the primary.") | |
Add-AzureRmVMNetworkInterface -VM $vm -Id $nic | Update-AzureRmVm -ResourceGroupName $resourcegroupname | |
# List existing NICs on the VM and find which one is primary | |
$vm.NetworkProfile.NetworkInterfaces | |
# Set NIC 2 to be primary | |
$vm.NetworkProfile.NetworkInterfaces[1].Primary = $true | |
# Verify | |
$vm.NetworkProfile.NetworkInterfaces | |
# Get the NIC to be removed | |
$nic2 = (Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroupname -Name $unwantednicname).Id | |
# Remove it | |
Remove-AzureRmVMNetworkInterface -VM $vm -NetworkInterfaceIDs $nic2 | |
# Verify | |
$vm.NetworkProfile.NetworkInterfaces | |
# Update the VM state in Azure (this takes time) | |
Update-AzureRmVM -VM $vm -ResourceGroupName $resourcegroupname | |
# Verify | |
$vm.NetworkProfile.NetworkInterfaces | |
# Enable "accelerated networking" | |
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $resourcegroupname -Name $wantednicname | |
$nic.EnableAcceleratedNetworking = $true | |
$nic | Set-AzureRmNetworkInterface | |
# You can now delete the removed NIC from the Azure Portal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment