Attaching an NIC to an Azure VM using Azure PowerShell

Since adding an NIC to an Azure VM requires the VM to be in a Stopped (Deallocated) state, you will have to run the following cmdlets:

$VM = Get-AzureRMVM -Name PacktPubVMPS -ResourceGroupName PacktPub
Stop-AzureRMVM -Name $VM -ResourceGroupName PacktPub

Next, you have to retrieve the subnet info to which the NIC will be assigned by running the following cmdlet:

$Subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name NSubnet
-VirtualNetwork $VNet

After that, you can create the additional NIC by running the following cmdlet:

$AdditionalNIC = New-AzureRmNetworkInterface -ResourceGroupName PacktPub -Name AddNICPS -Location WestEurope -SubnetId $Subnet.Id

You can also create the NIC using the same method discussed during the VM creation section earlier.

You're now done with NIC creation, but the NIC has not yet been added to the VM. To ensure that it is, you need to retrieve the NIC ID, which will be needed when you the NIC is added to the VM. Retrieve the ID by using the following cmdlet:

$NICId = (Get-AzureRmNetworkInterface -ResourceGroupName PacktPub -Name AddNIC).Id

Now, you can add the NIC to the VM by running the following cmdlet:

Add-AzureRmVMNetworkInterface -VM $VM -Id $NICId | Update-AzureRmVm
-ResourceGroupName PacktPub

Voila! The new NIC has now been successfully added to the VM.

Now you can start the VM by running the following cmdlet:

Start-AzureRMVM -Name PacktPubVMPS -ResourceGroupName PacktPub
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset