VMware Cloud Community
gpeck29
Enthusiast
Enthusiast

Custom Atrribute Access

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.

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kjb007
Immortal
Immortal

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

-KjB

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
Reply
0 Kudos
admin
Immortal
Immortal

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.

Reply
0 Kudos
Subatomic
Enthusiast
Enthusiast

Hi gpeck29

This will allow you to set the description field.

set-vm -description 'whatever the description is'

If the comments were useful, please consider awarding points for helpful or correct. Thanks - SA -
Reply
0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Subatomic
Enthusiast
Enthusiast

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

If the comments were useful, please consider awarding points for helpful or correct. Thanks - SA -
Reply
0 Kudos
admin
Immortal
Immortal

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
admin
Immortal
Immortal

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.

Reply
0 Kudos