I need to get the network interface card name from the resource Id.
vmsList=$(az vm list --show-details --query '[?name!=`null`].[name]' -o tsv)
for vm in ${vmsList[@]}
do
nics="$(az vm nic list --vm-name $vm --query "[].{id:id}" --output tsv)"
for nic in ${nics[@]}
do
az vm nic show --vm-name $vm --nic $nic --query '{Name:name,Location:location}' -o json
done
done
But it is giving us this error.
'/subscriptions/subscriptionName/resourceGroups/resourceGroupName/providers/Microsoft.Network/networkInterfaces/networkInterfaceCardName' not found on VM 'VMName'
ERROR: Operation returned an invalid status 'Bad Request'
I know this error is because we just need to pass only network interface name to az vm nic show
command. But I am stuck to get the resource name only from resource Id.
To get the NIC details of particular VM, use the below command
$virtualmachine = Get-AzVM -VMName "VMName"
$virtualmachine.NetworkProfile
$nic = $virtualmachine.NetworkProfile.NetworkInterfaces
foreach($nics in $nic) {
($nics.Id -split '/')[-1]
}
To get the NIC details of all the VM's in a ResourceGroup ,use
$virtualmachine= Get-AzVM -ResourceGroupName "YourRGName"
$virtualmachine.NetworkProfile
$nic = $virtualmachine.NetworkProfile.NetworkInterfaces
foreach($nics in $nic) {
($nics.Id -split '/')[-1]
}
References taken from :