I just worked it out, after you select the subscription via the cloud shell.
You can run the below script to get tags from the vm level under a specified subscription and then apply the same tag to the disks and network interface card that is associated with the vm. I have run it in my lab and works perfectly.
Notes: The customized script is Microsoft Best Effort support, it’s not guaranteed to be flawless, please test in your testing sub scription before use it directly in your product environment.
Copyright (c) Microsoft Corporation.MIT LicensePermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Script Details:
# Get the list of virtual machines
$vms = Get-AzVM
# Loop through each virtual machine
foreach ($vm in $vms) {
# Get VM details
$vmResourceGroup = $vm.ResourceGroupName
$vmName = $vm.Name
$vmId = $vm.Id
Write-Host "Processing VM: $vmName in Resource Group: $vmResourceGroup"
# Get VM Tags
$vmTags = (Get-AzResource -ResourceGroupName $vmResourceGroup -ResourceName $vmName -ResourceType "Microsoft.Compute/virtualMachines").Tags
# Get the VM's all disk resource ids
$diskIds = Get-AzDisk | Where-Object { $_.ManagedBy -eq $vmId } | Select-Object -ExpandProperty Id
# Update tags for disks
foreach ($diskId in $diskIds) {
Write-Host "Applying tags to Disk: $diskId"
Update-AzTag -ResourceId $diskId -Tag $vmTags -Operation Merge
}
# Get the VM's NIC (Network Interface) resource ids
$nicIds = $vm.NetworkProfile.NetworkInterfaces.Id
# Apply VM Tags to all NICs
foreach ($nicId in $nicIds) {
Write-Host "Applying tags to NIC: $nicId"
Update-AzTag -ResourceId $nicId -Tag $vmTags -Operation Merge
}
Write-Host "Completed processing VM: $vmName"
}