I know how to do this in Powershell, but I can't for the life of me figure out how to do this in Azure CLI.
I've spent hours poring over every post I can find tangentially related to this and tested lots of ideas based on them, but so for I've had no luck.
Things I have found out:
- pulling tags from an object in Azure using Azure CLI results in JSON output, unsurprisingly
- adding tags to an object in Azure using Azure CLI only supports a string in the format "key1=value1 key2=value2" - this is what boggles my mind, that they don't support JSON (or hashtable) input for tags. If Azure CLI supports any other format for tags, I can't find it documented anywhere.
Powershell is very simple:
$sourceobject = Get-AzResource -Name $sourceobjectname -ResourceGroupName $sourcergname
$tags = [hashtable] $((Get-AzTag -ResourceId $sourceobject.Id).Properties.TagsProperty)
$destobject = Get-AzResource -Name $destobjectname -ResourceGroupName $destrgname
Update-AzTag -ResourceId $destobject.id -Tag $tags -Operation Merge
If anyone has an Azure CLI equivalent, that would be very helpful.
I know how to do this in Powershell, but I can't for the life of me figure out how to do this in Azure CLI.
I've spent hours poring over every post I can find tangentially related to this and tested lots of ideas based on them, but so for I've had no luck.
Things I have found out:
- pulling tags from an object in Azure using Azure CLI results in JSON output, unsurprisingly
- adding tags to an object in Azure using Azure CLI only supports a string in the format "key1=value1 key2=value2" - this is what boggles my mind, that they don't support JSON (or hashtable) input for tags. If Azure CLI supports any other format for tags, I can't find it documented anywhere.
Powershell is very simple:
$sourceobject = Get-AzResource -Name $sourceobjectname -ResourceGroupName $sourcergname
$tags = [hashtable] $((Get-AzTag -ResourceId $sourceobject.Id).Properties.TagsProperty)
$destobject = Get-AzResource -Name $destobjectname -ResourceGroupName $destrgname
Update-AzTag -ResourceId $destobject.id -Tag $tags -Operation Merge
If anyone has an Azure CLI equivalent, that would be very helpful.
Share Improve this question asked Feb 4 at 0:55 pgbfnfpgbfnf 34 bronze badges1 Answer
Reset to default 0Not elegant but works. Attention to my comments.
# need powershell >= 6. otherwise, ConvertFrom-Json -AsHashTable cannot support -AsHashTable argument.
## step1: get original tags
$oriRgName = "wb-test-once-rg"
$group1 = $(az group show -n $oriRgName --query id --output tsv)
$tags = az tag list --resource-id $group1
$tagsObj = $($tags | ConvertFrom-Json).properties.tags
$tagsHash = $tagsObj | ConvertTo-Json | ConvertFrom-Json -AsHashTable
## step2: change the pscustomobject data to array. (using hashtable key values.)
### Convert to a custom object array
$array = @()
foreach ($key in $tagsHash.Keys) {
$obj = "$key = $($tagsHash[$key])"
$array += $obj
}
$array
## step3: update the tags using array.
$desRgName = "wb-test-tags-rg"
$group2 = $(az group show -n $desRgName --query id --output tsv)
az tag create --resource-id $group2 --tags $array