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
} |
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
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
Works like a charm, Thank you!