'Azure Powershell - Get ONLY one tag (key,value) of a resource and then apply that to another resource that is missing it
I have a resource list filled with VMs. I need to get first the tag of the corresponding NICs because the tags of the VMs were accidentally removed, the NIC tags are still there. Then I need to reapply only the 'Monitored' tag over the corresponding VM. I am confused with arrays.
Foreach ($Resource in $ResourceList)
{
$VM = Get-AzVM -Name $Resource
$NICs = $VM.NetworkProfile.NetworkInterfaces
Foreach($NIC in $NICs) {
$CurrentNIC = $NIC.Id
#CurrentTag should be filled with Get-AzResource instead of Az-Tag
$CurrentResource = Get-AzResource -ResourceId $CurrentNIC
$CurrentTag = $CurrentResource.Tags
#Get tag "Monitored" from $CurrentTag variable
$ResultsList += New-Object PsObject -property @{
'Monitored' = $CurrentResource.Monitored
}
#Apply Monitored tag over the current VM
Update-AzTag -ResourceId $VM.Id -Tag $ResultsList -Operation Merge
}
}
}
$ResultsList += New-Object PsObject -property @{
'Monitored' = $CurrentResource.Monitored
}
It is getting populated with :
Monitored
-
-------------------------
Instead of :
Tags:
Name Value
Monitored default
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
