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
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.
$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
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.
$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
Thanks Luc!
Much appreciated!
-Chris
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
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
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
Can you run the following for one the VMs where that CA is set?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Get-Annotation -Entity smith-test-vm -Name owner
AnnotatedEntity Name Value
--------------- ---- -----
smith-test-vm owner smith@smith.com
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
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
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
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