PowerCLI

 View Only
  • 1.  PowerCLI & vmtools custom attributes

    Posted Aug 12, 2022 06:04 PM

    I'm using power cli to set a custom attribute on a virtual machine, and then I'd like to be able to retrieve the value of the custom attribute from the virtual machine itself using vmtools. Can anyone explain why it's returning no value when the custom attribute is visible via powerCLI and also in vSphere under the VM custom attributes?

     

     

    > Set-Annotation -CustomAttribute "guestinfo.esx" -Entity "virtualmachine1" -Value esxi-host-1
    
    AnnotatedEntity Name Value
    --------------- ---- -----
    virtualmachine1 guestinfo.esx esxi-host-1
    
    > Get-VM virtualmachine1 | Get-Annotation -CustomAttribute guestinfo.esx
    
    AnnotatedEntity Name                 Value
    --------------- ----                 -----
    virtualmachine1 guestinfo.esx        esxi-host-1
    
    
    [root@virtal-machine-1 ~]# vmtoolsd --cmd "info-get guestinfo.esx"
    No value found

     

     

     

    ctrought_0-1660327379059.png

      



  • 2.  RE: PowerCLI & vmtools custom attributes
    Best Answer

    Posted Aug 13, 2022 11:42 AM

    Hi.

    There was already a similar topic.

    https://community.broadcom.com/vmware-cloud-foundation/discussion/how-to-get-esxi-host-name-within-running-vm#bm7638469a-5c82-42e1-86ff-94899cb3f74a

    It is possible though to set a custom attribute on the VM object in vCenter and then query this from within the the OS of the virtual machine.
    For example:

    $vmView = GET-VM -Name 'Vm_Name' | Get-View
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $gInfo = New-Object VMware.Vim.optionvalue
    $ginfo.Key=”guestinfo.hostname”
    $gInfo.Value=$vmView.Name
    $vmConfigSpec.extraconfig += $gInfo
    $vmView.ReconfigVM($vmConfigSpec)

     

    Once this is set, it is possible to query it within the VM, using the VM tools and the following command:

    vmtoolsd -–cmd “info-get guestinfo.hostname”

     



  • 3.  RE: PowerCLI & vmtools custom attributes

    Posted Aug 15, 2022 09:09 PM

    Thanks a lot, that works perfectly!