Export-CSV showing System.Collections.Generic.Dictionary`2[System.String,System.String] for Azure Tags?

Viewed 1246

My script using Get-AzDisk https://docs.microsoft.com/en-us/powershell/module/az.compute/get-azdisk?view=azps-5.9.0 like below

Get-AzDisk -ResourceGroupName $ResourceGroupName|Where-Object{$_.ManagedBy.Length -lt 1} | Select-Object Name, OsType, DiskSizeGB, ResourceGroupName, Location, Tags, Tier, ProvisioningState, TimeCreated | ogv

Cannot export the actual Tags values like in the Out-GridView does:

enter image description here

This is the .CSV column for Tags:

enter image description here

How to modify it so it can show the same data as in the OGV?

2 Answers

You could use a calculated property to format the object (which is a hashtable) to the desired output. Here is an example that uses an calculated property to serialize the tags as JSON:

Get-AzDisk -ResourceGroupName $ResourceGroupName 
| Where-Object { $_.ManagedBy.Length -lt 1 } 
| Select-Object Name, OsType, DiskSizeGB, ResourceGroupName, Location, @{e = {$_.Tags | ConvertTo-Json | Out-String}; l='Tags'} , Tier, ProvisioningState, TimeCreated

And here the corresponding output as CSV (after I pipe it to ConvertTo-CSV cmdlet): enter image description here

Tags are a Hash.

$myTags = $vm.Tags
if($myTags) {
    foreach ($key in $myTags.Keys) {
        $curVal = $myTags[$key]
    }
}

You can grab all the Keys and Values and write them to a string in the column. Be aware the $curVal could itself be a multi-value system.collections.generic.dictionary and might require further manipulation.

Related