VMware Cloud Community
Moras
Contributor
Contributor
Jump to solution

Exporting VM tags with category for all VM's

Hello,

I'm struggling with the below script to show in the output all VM's with and without assigned tags & category. Is there any simple way to achieve this? Currently it shows only those that have a tag assigned. Thanks!

 

$objTemplate = @{

VM = ''

}

(Get-TagCategory).Name | Sort-Object | ForEach-Object -Process {

$objTemplate.Add($_,'')

}

Get-TagAssignment|

Group-Object -Property {$_.Entity.Name} |

ForEach-Object -Process {

$obj = $objTemplate.Clone()

$obj['VM'] = $_.Name

$_.Group | Group-Object -Property {$_.Tag.Category.Name} |

ForEach-Object -Process {

$cat = $_.Name

$vTag = $_.Group | Group-Object -Property {$_.Tag.Category.Name} |

ForEach-Object -Process {

$_.Group.Tag.Name

}

$obj[$cat] = (($vTag | Sort-Object) -join '|')

}

New-Object PSObject -Property $obj

} |

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$cTag = Get-TagCategory | Sort-Object -Property Name
Get-VM -PipelineVariable vm | 
ForEach-Object -Process {
    $obj = [ordered]@{
        VM = $_.Name
    }
    $cTag | ForEach-Object -Process {
        $value = (Get-TagAssignment -Entity $vm -Category $_).Tag.Name -join '|'
        $obj.Add($_.Name, $value)
    }
    New-Object -TypeName PSObject -Property $obj
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$cTag = Get-TagCategory | Sort-Object -Property Name
Get-VM -PipelineVariable vm | 
ForEach-Object -Process {
    $obj = [ordered]@{
        VM = $_.Name
    }
    $cTag | ForEach-Object -Process {
        $value = (Get-TagAssignment -Entity $vm -Category $_).Tag.Name -join '|'
        $obj.Add($_.Name, $value)
    }
    New-Object -TypeName PSObject -Property $obj
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Moras
Contributor
Contributor
Jump to solution

Works like a charm, Thank you!

0 Kudos