Automation

 View Only
Expand all | Collapse all

How to search for VMs matching a specific custom attribute

  • 1.  How to search for VMs matching a specific custom attribute

    Posted Sep 11, 2020 12:42 AM

    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



  • 2.  RE: How to search for VMs matching a specific custom attribute
    Best Answer

    Posted Sep 11, 2020 05:39 AM

    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



  • 3.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 11, 2020 07:22 PM

    Thanks Luc!

    Much appreciated!

    -Chris



  • 4.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 14, 2020 04:54 AM

    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.



  • 5.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 14, 2020 04:37 PM

    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"

    Thanks in advance!

    -Chris



  • 6.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 14, 2020 04:47 PM

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

    Get-Annotation -Entity <VMname> -Name owner


  • 7.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 14, 2020 05:22 PM

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

    AnnotatedEntity Name                 Value

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

    smith-test-vm   owner                smith@smith.com



  • 8.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 14, 2020 05:55 PM

    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?



  • 9.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 15, 2020 10:10 PM

    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



  • 10.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 16, 2020 05:24 AM

    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.



  • 11.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 16, 2020 03:47 PM

    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



  • 12.  RE: How to search for VMs matching a specific custom attribute

    Posted Sep 13, 2020 11:34 PM

    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