How to retrieve the Azure VM NIC name using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To retrieve the Azure VM NIC using PowerShell, we need to first get the VM details. For this example, we have the VM name “TestMachine2k16”. To retrieve the VM details use the Get-AzVM command but before that make sure you are connected to the Azure Account using PowerShell session.

PS C:\> $vm = Get-AzVM -VMName Testmachine2k16

VM NIC information is stored inside the NetworkProfile property.

PS C:\> $vm.NetworkProfile

This will retrieve all the NICs attached to the VM. If there are multiple NICs then we need to store the nic information into the array and have to perform some string operation to get the NIC details.

$nics = $vm.NetworkProfile.NetworkInterfaces

To retrieve the NIC details.

foreach($nic in $nics) {
   ($nic.Id -split '/')[-1]
}

The above output will retrieve the NICs attached to the VM.

raja
Published on 28-Apr-2021 17:14:00
Advertisements