VMware Cloud Community
Tenchuu
Contributor
Contributor

Filtering by Annotations

Hi,

I was wondering if anyone would know the correct syntax for setting the filter for Annotations.

Basically what I want to do is adding an annotation, let's say "Cost Center" and then get only those VMs which are in a certain cost center.

It appears like it accepts my syntax but I won't get any results.

var Filter = new NameValueCollection();

Filter.Add("CustomValue[0]", costCenter);

var Results = Client.FindEntityViews(typeof(VirtualMachine), null, Filter, null);

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

Are you sure this is a PowerCLI question ?


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

Reply
0 Kudos
Tenchuu
Contributor
Contributor

Why wouldn't it be?

Btw. if you could tell me how it works in PowerShell, it might as well work in c# - couldn't find it for PowerShell either though.

Reply
0 Kudos
Nerenther
Contributor
Contributor

To my knowledge, there is no filter for annotations in PowerCLI. You need to make use of native Powershell commands like where-object and select-object.

The slow way:

foreach ($vm in (Get-View -ViewType virtualmachine -Property name)) {Get-Annotation -Entity $vm.name -CustomAttribute "YourCustomAttribute" | where-object {$_.value -eq "SomeValue"}}

The faster way:

Get-View -ViewType virtualmachine -Property name,customvalue | where-object {($_.CustomValue.Key -eq "YourCustomAttributesKey") -and ($_.CustomValue.Value -eq "SomeValue")} | select-object name -ExpandProperty customvalue | where-object {$_.key -eq "YourCustomAttributesKey"}

I believe the easiest way to get the key of your custom attribute is to check a vm that has a value in that attribute:

Get-View -ViewType virtualmachine -Filter @{"Name" = "NameOfVM"} | Select-Object -ExpandProperty customvalue

Reply
0 Kudos
Tenchuu
Contributor
Contributor

Thanks for your reply.

Unfortunately that wouldn't work because it doesn't filter the query, it pulls all the VMs first and then you filter the output.

I guess I'll just have to cache the VMs then, otherwise I'll run into performance issues.

Reply
0 Kudos
BrianDGeorge
Enthusiast
Enthusiast

It took a long time to run and the report came out great but still included all the results, not just blanks.

Reply
0 Kudos