VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Find VMs only where Specific Custom attribute is missing

Hello

I have below custom attributes in vCenter

 

esxi1979_0-1614745110253.png

 

 

At times folks create VM & forget to updates these

 

I have code like below

 

Get-VM $($line.name) |Get-Annotation -CustomAttribute "OS"

 

But i would like to filter t9o get VM names , only if the value is blank for any said custom attributes

 

Please suggest

 

Thanks

 

 

 

 

0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VM | 
where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} |
Select Name


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

View solution in original post

LucD
Leadership
Leadership
Jump to solution

That requires a slightly different Where-clause.

Get-VM |
Where{(Get-Annotation -Entity $_ -Customattribute 'OS', 'LOB').Value.Foreach{ $_ -eq '' } -contains $true} |
Select Name


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this

Get-VM | 
where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} |
Select Name


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

esxi1979
Expert
Expert
Jump to solution

Ah thanks @LucD  i was trying like below was not working

 

Get-VMHost -location xxx | Get-VM |Get-Annotation -CustomAttribute "OS" | Where-Object { $_.OS -notlike '' }

0 Kudos
esxi1979
Expert
Expert
Jump to solution

Can i add other attributes in code like

 

where{(Get-Annotation -Entity $_ -CustomAttribute OS).Value -eq ''} or where{(Get-Annotation -Entity $_ -CustomAttribute LOB).Value -eq ''} 

 

etc ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can add multiple customa ttributes on the CustomAttribute parameter.
The following will list the VM if at least one of the custom attributes does not have a value

Get-VM |
Where{(Get-Annotation -Entity $_ -Customattribute 'OS', 'LOB').Value.Foreach{ $_ -ne '' } -notcontains $true} |
Select Name

 


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

esxi1979
Expert
Expert
Jump to solution

@LucD  thanks for the reply

 

I tested with 2 vms , both are power on

 

One has only OS custom attribute set

Other VM has none set

 

But below code only reported the one which has all blank .. so looks like the code used "AND" vs "OR"

In my case i need names of all vms where either of the attributes is not set ( ie if any vm has both attributes set they are good & i do not care about them )

Please suggest

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That requires a slightly different Where-clause.

Get-VM |
Where{(Get-Annotation -Entity $_ -Customattribute 'OS', 'LOB').Value.Foreach{ $_ -eq '' } -contains $true} |
Select Name


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