9 Replies Latest reply on Apr 18, 2008 11:34 AM by adias

    Custom Atrribute Access

    gpeck29 Enthusiast

       

      In VIrtualCenter 2.5, you have the ability to create Annotations in the "description/annotations" part of the VM. Is there a way to access these in PowerShell? I ask, as I would like to have a custom attribute that would be called "ReleaseSnapshot" and if the value is Y or TRUE, then take a snap before code is released. Otherwise skip the VM.

       

       

       

       

       

       

       

       

        • 1. Re: Custom Atrribute Access
          LucD Guru
          Community WarriorsvExpertUser Moderators

          The attributes can't be accessed directly (afaik) but by getting the virtualmachine object you can access and test the attributes.

           

          In the virtualmachine object there are 2 arrays:

          - AvailableField: contains all the available custom attributes. Here we have to get the "key" of the attribute we want

          - CustomValue: contains all the values for the custom attributes. We have to use the "key" to find the desired value

           

          The following script shows how this can be done from within the VI Toolkit

           

          $VCimpl = Get-VIServer -Server <VCserver>
          
          $tgtVM = "VMName"
          $tgtName = "ReleaseSnapshot"
          $tgtValue = "yes"
          
          $vm = Get-View (Get-VM -Name $tgtVM).ID
          foreach ($fld in $vm.AvailableField){
            if ($fld.Name -eq $tgtName) {
              $tgt = $fld.Key
            }
          }
          foreach ($val in $vm.CustomValue){
            if ($val.Key -eq $tgt){
              if ($val.Valye -eq $tgtValue){
                 $retValue = $TRUE
               }
            }
          }
          

           

          You could package this logic in a function that you can then use inside a pipe-construct.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Custom Atrribute Access
            kjb007 Guru

             

            You could edit the notes/description part of the vm, which is accesible to powershell.  Is that what you wanted.

             

             

             

             

             

            -KjB

             

             

            • 3. Re: Custom Atrribute Access
              Enthusiast

              They should be available. Try:

               

              Get-Command *CustomField

               

              And individual objects (e.g. the result of Get-VM) should have a CustomFields property.

               

              If you have trouble with this, please let us know.

              • 4. Re: Custom Atrribute Access
                Subatomic Hot Shot

                Hi gpeck29

                 

                This will allow you to set the description field.

                 

                 

                set-vm -description 'whatever the description is'

                • 5. Re: Custom Atrribute Access
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  I suspect there is some confusion between "Attributes" and "Notes" in the Annotations field of a guest.

                   

                  The "Notes" you can indeed access/set via the -Description parameter in a number of guest related cmdlets.

                  For the "Attributes" I didn't find a straightforward way of accessing/settings these in any cmdlet in the Toolkit.

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Custom Atrribute Access
                    Subatomic Hot Shot

                    Thanks for the clarification.  (I'm still quite new to this)

                    • 7. Re: Custom Atrribute Access
                      Enthusiast

                      The *CustomField cmdlets are supposed to help you manage the "attributes." If they're not clear enough or not working as expected, please do let us know.

                      • 8. Re: Custom Atrribute Access
                        LucD Guru
                        vExpertCommunity WarriorsUser Moderators

                        You are right, the *CustomField cmdlets allow us to set and manipulate the "attributes".

                        I must have missed those.

                        No need to access the entity objects directly.

                         

                        The only confusion could be the fact that the VI Client calls them Attributes while the Toolkit uses the term CustomField

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 9. Re: Custom Atrribute Access
                          Enthusiast

                          Right. We use one name in the UI and another name in the API. Need to consider whether we should change the name of the cmdlet.