DBA-One
DBA-One

Reputation: 51

Powershell question about parsing the tag values in Get-AzVm

I'm attempting to load individual tag key and value records per VM using the Get-AzVm cmdlet. The values are stored like:

"Tags : {"Purpose":"SQL Server","Test":"Value"}"

I want to load them like:

VMID, VMName, Key, Value

No amount of searching or testing with ForEach, ForEach-Object or loading in to a hash is working as the results are always null, but what is loaded in to a variable is not. I would be very grateful for any suggestions.

Upvotes: 2

Views: 962

Answers (1)

DBA-One
DBA-One

Reputation: 51

    $vm_list = Get-AzVM -Name #######
foreach ($name in $vm_list)
    {
    $_ = $vm_list.tags.GetEnumerator() | 
    ForEach-Object{
                $k = $_.key
                $v = $_.value
                Write-Host $tagkeys.VMID, $tagkeys.Name, $k, $v
                }
   }

There is more to the script, but this is what I have working now. Using enumerator, I would have expected to need to reference the objects as {0} and {1}.

Upvotes: 3

Related Questions