PowerCLI

  • 1.  VM Info based on Customattribute

    Posted Feb 04, 2025 09:40 AM

    Hi,

    I am trying to get VM info based on the custom attributes, but below script shows blank, please help!!

    Get-VM | Get-Annotation -CustomAttribute "RFC" | Where {$_.Value -eq "RFC-23069"} | Select-Object @{Name="Folder";E={$_.Parent.Folder}}, 
    @{Name="Name";E={$_.Parent.Name}},
    @{N="IP_Address";E={@($_.parent.guest.IPAddress[0])}},
    @{N="NumCPU"; E={@($_.parent.NumCPU)}},
    @{N="MemoryGB"; E={@($_.parent.MemoryGB)}},
    @{N="OS"; E={@($_.parent.guest.OSFullName)}},
    @{N="RFC"; E={@($_.Name)}},
    @{N="OS"; E={@($_.parent.guest.OSFullName)}},
    @{N="VMHost"; E={@($_.parent.VMHost)}},
    @{N="HDD"; E={@($_ | Get-HardDisk | Where-Object {$_.Name -like "*Hard disk 2*"}).CapacityGB)}} | ft -auto



  • 2.  RE: VM Info based on Customattribute
    Best Answer

    Posted Feb 04, 2025 11:04 AM

    Instead of the Parent property, you should use the AnnotatedEntity property.
    And you have the OS calculated property twice in there.
    The parenthesis in the HDD calculated property are placed incorrectly.

    Get-VM | Get-Annotation -CustomAttribute "RFC" |
    Where-Object { $_.Value -eq "RFC-23069" } |
    Select-Object @{Name = "Folder"; E = { $_.AnnotatedEntity.Folder } },
    @{Name = "Name"; E = { $_.AnnotatedEntity.Name } },
    @{N = "IP_Address"; E = { @($_.AnnotatedEntity.guest.IPAddress[0]) } },
    @{N = "NumCPU"; E = { @($_.AnnotatedEntity.NumCPU) } },
    @{N = "MemoryGB"; E = { @($_.AnnotatedEntity.MemoryGB) } },
    @{N = "OS"; E = { @($_.AnnotatedEntity.guest.OSFullName) } },
    @{N = "RFC"; E = { @($_.Name) } },
    @{N = "VMHost"; E = { @($_.parent.VMHost) } },
    @{N = "HDD"; E = { ($_ | Get-HardDisk -VM $_.AnnotatedEntity  |
        Where-Object { $_.Name -like "*Hard disk 2*" }).CapacityGB } } |
     Format-Table -auto


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


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


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



  • 3.  RE: VM Info based on Customattribute

    Posted Feb 04, 2025 12:09 PM

    Thank you LucD, that worked as expected.

    Get-VM | Get-Annotation -CustomAttribute "RFC" |
    Where-Object { $_.Value -eq "RFC-23069"} | Select-Object @{Name="Folder";E={$_.AnnotatedEntity.Folder}},
    @{Name="Name";E={$_.AnnotatedEntity.Name}},
    @{N="IP_Address";E={@($_.AnnotatedEntity.guest.IPAddress[0])}},
    @{N="NumCPU";E={@($_.AnnotatedEntity.NumCPU)}},
    @{N="MemoryGB";E={@($_.AnnotatedEntity.MemoryGB)}},
    @{N="HDD(GB)";E={(Get-HardDisk -VM $_.AnnotatedEntity | Where-Object {$_.Name -like "*Hard disk 2*"}).CapacityGB}},
    @{N="OS";E={@($_.AnnotatedEntity.guest.OSFullName)}},
    @{N="RFC";E={@($_.Value)}},
    @{N="VMHost";E={@($_.AnnotatedEntity.VMHost)}} | Format-Table -auto