VMware Cloud Community
monderick
Enthusiast
Enthusiast
Jump to solution

slowness of get-annotation

made a script to find a VM based upon a specific annotation but takes ~40 seconds to cycle through ~800 VMs.  already exempting many more VMs by name filtering and annotations i don't care about but not seeing a way to use get-view command which is generally faster.  any ideas?

get-cluster "Workstations Cluster" | get-vm | ?{ $_.name -match "test" } | get-annotation -CustomAttribute ("Owner") | where-object { $_.Value -eq "testuser" }

AnnotatedEntity          Name         Value
---------------                  ----              -----
testvm                          Owner        testuser

 

thanks!

 

s!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$caName = 'Owner'
$caValue = 'testuser'
$clusterName = 'Workstation Cluster'

$ca = Get-CustomAttribute -Name $caName
$cl = Get-View -ViewType ClusterComputeResource -Filter @{Name='cluster'} -Property Name
Get-View -ViewType VirtualMachine -SearchRoot $cl.MoRef  -Property Name,Value |
where{$_.Value.where{$_.Key -eq $ca.Key -and $_.Value -eq $caValue}} |
Select Name


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$caName = 'Owner'
$caValue = 'testuser'
$clusterName = 'Workstation Cluster'

$ca = Get-CustomAttribute -Name $caName
$cl = Get-View -ViewType ClusterComputeResource -Filter @{Name='cluster'} -Property Name
Get-View -ViewType VirtualMachine -SearchRoot $cl.MoRef  -Property Name,Value |
where{$_.Value.where{$_.Key -eq $ca.Key -and $_.Value -eq $caValue}} |
Select Name


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

monderick
Enthusiast
Enthusiast
Jump to solution

cheers, Luc!  amazing as always 

i was getting an error on $cl.MoRef  (Get-View : Cannot convert 'System.Object[]' to the type 'VMware.Vim.ManagedObjectReference' required by parameter 'SearchRoot'. Specified method is not supported). 

tinkered a bit and it didn't seem to like something with Filter @{Name='cluster'} but works great without that part.  

$caName = 'Owner'
$caValue = 'testuser'
$clusterName = 'Workstation Cluster'

$ca = Get-CustomAttribute -Name $caName
$cl = Get-View -ViewType ClusterComputeResource -Property Name
Get-View -ViewType VirtualMachine -SearchRoot $cl.name.MoRef -Property Name,Value | where{$_.Value.where{$_.Key -eq $ca.Key -and $_.Value -eq $caValue}} | Select Name 

 

my original command:     TotalSeconds : 24.0449337

your way:                         TotalSeconds : 0.5140654

😲

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That error seems to indicate that multiple cluster objects were returned.
Since the Filter uses a RegEx, you can try an exact match.
That might fix the error.

$cl = Get-View -ViewType ClusterComputeResource -Filter @{Name="^cluster$"} -Property Name

 


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

Reply
0 Kudos