chrischrischris
Enthusiast
Enthusiast

How to search for VMs matching a specific custom attribute

Jump to solution

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)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

0 Kudos
11 Replies
LucD
Leadership
Leadership

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

0 Kudos
chrischrischris
Enthusiast
Enthusiast

Thanks Luc!

Much appreciated!

-Chris

0 Kudos
chrischrischris
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
chrischrischris
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
chrischrischris
Enthusiast
Enthusiast

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

AnnotatedEntity Name                 Value

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

smith-test-vm   owner                smith@smith.com

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
chrischrischris
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
chrischrischris
Enthusiast
Enthusiast

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

0 Kudos