I have inherited an esx environment, and it uses tags, instead of annotations. (I understand that annotations are depreciating.)
A script used monthly to get a csv of information, mainly for billing, does not return crucial data on the new environment.
I can see the tags in the GUI but when I run:
get-vm servername |select *|fl
I don't get the tags.
If I run this, I don;t get all of them either.
Get-vm | Get-TagAssignment |Select -ExpandProperty Entity
How can I get a list of all vm's with all of their associated tags and basic stuff like Name,PowerState,DNSName ?
ty!
You can use an [ordered] object, then the properties stay in the order you want.
$tagTab = @{ }
$vmTab = @{ }
foreach ($tag in (Get-VM | Get-TagAssignment)) {
$tagCat += $tag.Tag.Category.Name
$key = $tag.Entity.Name
if ($tagTab.ContainsKey($key)) {
$val = $tagTab.Item($key)
} else {
$val = @{ }
}
$val.Add($tag.Tag.Category.Name, $tag.Tag.Name)
$tagTab[$key] = $val
if (-not $vmTab.ContainsKey($tag.Entity.Name)) {
$vmTab.Add($tag.Entity.Name, $tag.Entity)
}
}
$tagCat = $tagCat | Sort-Object -Unique
$tags = foreach ($row in ($tagTab.GetEnumerator() | Sort-Object -Property Key)) {
$obj = [ordered]@{
VM = $row.Key
PowerState = $vmTab[$row.Key].PowerState
IP = $vmTab[$row.Key].Guest.IPAddress -join '|'
}
$tagCat | ForEach-Object {
$obj.Add($_, $row.Value[$_])
}
New-Object PSObject -Property $obj
}
$tags |
Export-Csv .\$(((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))info.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What tags are missing when you do
Get-vm | Get-TagAssignment
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh . ok.. so tags are like groups in a sense and items are in them are given a value (entity/server name)
Tag Entity
--- ------
VM - tag1 server1
VM - tag2 server1
VM - tag3 server1
VM - tag4 server1
Is there a simple script to get all vms with their tags like this
Entity tag1 tag2
-------- -------- --------
server info info
That is possible, but what goes in the 'info' part?
Contrary to a Custom Attribute a Tag does not hold a value, it's kind of like a label.
Tags also belong to a Category. And such a Category has a Cardinality, meaning an entity can have 1 or more Tags from that Category.
Have a look at Exporting VM Tags in readable format
That's an example of how Tags can be reported upon.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Your script in the discussion worked well for the tags, thank you.
Can attributes such as GuestOS IPAddress be gathered as well?
In the same script you mean?
Sure, under the Entity property on the object returned by Get-TagAssignment, you have access to all the regular properties available on a .NET VirtualMachine object.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is what I'm using, I tried and failed to get anything to show up. Can you help with one or two properties, ipaddress and powerstate I can then add all the rest.
$tagCat = @()
$tagTab = @{}
foreach($tag in (Get-VM | Get-TagAssignment)){
$tagCat += $tag.Tag.Category.Name
$key = $tag.Entity.Name
if($tagTab.ContainsKey($key)){
` $val = $tagTab.Item($key)
}
else{
$val = @{}
}
$val.Add($tag.Tag.Category.Name,$tag.Tag.Name)
$tagTab[$key] = $val
}
$tagCat = $tagCat | Sort-Object -Unique
$tags = foreach($row in ($tagTab.GetEnumerator() | Sort-Object -Property Key)){
$obj = New-Object PSObject -Property @{
VM = $row.Key
}
$tagCat | %{
$obj | Add-Member -Name $_ -Value $row.Value[$_] -MemberType NoteProperty
}
$obj
}
$tags | Export-Csv .\$(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))info.csv -NoTypeInformation -UseCulture
You could do something like this
$tagTab = @{}
$vmTab = @{}
foreach ($tag in (Get-VM | Get-TagAssignment)) {
$tagCat += $tag.Tag.Category.Name
$key = $tag.Entity.Name
if ($tagTab.ContainsKey($key)) {
$val = $tagTab.Item($key)
}
else {
$val = @{}
}
$val.Add($tag.Tag.Category.Name, $tag.Tag.Name)
$tagTab[$key] = $val
if(-not $vmTab.ContainsKey($tag.Entity.Name)){
$vmTab.Add($tag.Entity.Name,$tag.Entity)
}
}
$tagCat = $tagCat | Sort-Object -Unique
$tags = foreach ($row in ($tagTab.GetEnumerator() | Sort-Object -Property Key)) {
$obj = New-Object PSObject -Property @{
VM = $row.Key
PowerState = $vmTab[$row.Key].PowerState
IP = $vmTab[$row.Key].Guest.IPAddress -join '|'
}
$tagCat | ForEach-Object {
$obj | Add-Member -Name $_ -Value $row.Value[$_] -MemberType NoteProperty
}
$obj
}
$tags | Export-Csv .\$(((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))info.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much, this is very close, how can I reorder the columns?
I tried added a sort-object after $tags, but that failed.
You can use an [ordered] object, then the properties stay in the order you want.
$tagTab = @{ }
$vmTab = @{ }
foreach ($tag in (Get-VM | Get-TagAssignment)) {
$tagCat += $tag.Tag.Category.Name
$key = $tag.Entity.Name
if ($tagTab.ContainsKey($key)) {
$val = $tagTab.Item($key)
} else {
$val = @{ }
}
$val.Add($tag.Tag.Category.Name, $tag.Tag.Name)
$tagTab[$key] = $val
if (-not $vmTab.ContainsKey($tag.Entity.Name)) {
$vmTab.Add($tag.Entity.Name, $tag.Entity)
}
}
$tagCat = $tagCat | Sort-Object -Unique
$tags = foreach ($row in ($tagTab.GetEnumerator() | Sort-Object -Property Key)) {
$obj = [ordered]@{
VM = $row.Key
PowerState = $vmTab[$row.Key].PowerState
IP = $vmTab[$row.Key].Guest.IPAddress -join '|'
}
$tagCat | ForEach-Object {
$obj.Add($_, $row.Value[$_])
}
New-Object PSObject -Property $obj
}
$tags |
Export-Csv .\$(((Get-Date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ"))info.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As usual, you are a miracle worker Thank you!