VMware Cloud Community
chrischrischris
Enthusiast
Enthusiast
Jump to solution

How to search for VMs matching a specific custom attribute

Hi all,

Our VMs have a custom attribute called owner that has the VM owner's e-mail address in it.

Is there a way to search for all VMs such that owner EQ a specific string without having to retrieve the attributes for every VM in our inventory?

Thanks in advance!

-Chris

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not really, you would still need to look at all the VMs.
The fastest way, as far as I can tell, is something like this.

$caName = 'Owner'

$caValue = 'user@domain'


$exp = {$_.CustomValue.Where({$_.Key -eq $caEntry.Key -and $_.Value -eq $caValue})}

$caMgr = Get-View CustomFieldsManager

$caEntry = $caMgr.Field.Where({$_.Name -eq $caName})

(Get-View -ViewType VirtualMachine -Property Name,CustomValue).Where($exp) |

Select Name


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

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Not really, you would still need to look at all the VMs.
The fastest way, as far as I can tell, is something like this.

$caName = 'Owner'

$caValue = 'user@domain'


$exp = {$_.CustomValue.Where({$_.Key -eq $caEntry.Key -and $_.Value -eq $caValue})}

$caMgr = Get-View CustomFieldsManager

$caEntry = $caMgr.Field.Where({$_.Name -eq $caName})

(Get-View -ViewType VirtualMachine -Property Name,CustomValue).Where($exp) |

Select Name


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

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Thanks Luc!

Much appreciated!

-Chris

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Luc,

I tried the code and it did not work: no output at all.

I'm not sure what is broken as the code is a bit beyond my understanding :-<

Best,

-Chris

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you assign the value 'Owner' to $caName and the value you are looking for in that Custom Attribute to $caValue?

Just tested this again, and for me, it is working.


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

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

Any help you can provide would be greatly appreciated.

Here is the code I'm using:

$caName = 'owner'

$caValue = 'smith@smith.com'

$exp = {$_.CustomValue.Where({$_.Key -eq $caEntry.Key -and $_.Value -eq $caValue})}

$caMgr = Get-View CustomFieldsManager

$caEntry = $caMgr.Field.Where({$_.Name -eq $caName})

(Get-View -ViewType VirtualMachine -Property Name,CustomValue).Where($exp) | Select Name

Here is custom attribute set owner = "smith@smith.com"

pastedImage_0.png

Thanks in advance!

-Chris

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you run the following for one the VMs where that CA is set?

Get-Annotation -Entity <VMname> -Name owner


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

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Get-Annotation -Entity smith-test-vm -Name owner

AnnotatedEntity Name                 Value

--------------- ----                 -----

smith-test-vm   owner                smith@smith.com

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Exactly the same as what I have, and for me it works.

I have no clue why that script wouldn't be working in your environment.

Which PowerShell, PowerCLI, vSphere version are you using?


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

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

HI Luc,

6.7.0 and

pwsh >  $PSVersionTable

Name                           Value

----                           -----

PSVersion                      7.0.3

PSEdition                      Core

GitCommitId                    7.0.3

OS                             Darwin 19.6.0 Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141…

Platform                       Unix

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

WSManStackVersion              3.0

Best,

-Chris

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Besides the fact that I'm financially unable to run Darwin :smileygrin:, I have no knowledge of any major issues on that OS.

I tested my script again with multiple test data sets, and it seems to work for me each time.

I have no clue what goes wrong with the script in your environment.


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

Reply
0 Kudos
chrischrischris
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I tested it again.

It works  if I'm logged into one vCenter.

It fails if I'm logged into multiple vCenters -- so I'll just need to have it loop through all our vCenters.

Thanks again -- really happy with the code!

-Chris

Reply
0 Kudos