Automation

 View Only
  • 1.  slowness of get-annotation

    Posted Jan 23, 2024 03:25 PM

    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!



  • 2.  RE: slowness of get-annotation
    Best Answer

    Posted Jan 23, 2024 07:20 PM

    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
    


  • 3.  RE: slowness of get-annotation

    Posted Jan 24, 2024 12:44 AM

    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

     



  • 4.  RE: slowness of get-annotation

    Posted Jan 24, 2024 07:29 AM

    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