VMware Cloud Community
marc045
Contributor
Contributor
Jump to solution

Setting annotations / custom fields directly via VM object

Hi All,

How can I set the value of an annotation / customfield directly via the VM object?

$myVM.CustomFields holds all the customfields, how do I update a value via $myVM.CustomFields?

e.g. To retrieve a value I can do:

$myValue = $myVM.customfields | where { $_.key -eq 'myField' } | select -expandproperty value

This is significantly faster than get-annotation.

How do I SET the value via the same mechanism?

Regards

marc0

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, marc045-

Once you have the VM object, you should be able to change/update an existing custom value by using the .Net View object's method, "setCustomValue".  You use this method as such:

$myVM.ExtensionData.setCustomValue("myField", "sweet new value")

The method takes two (2) strings:  the key name, and the value to set for that key.  You can find this info by browsing the vSphere API reference at http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html, or by using the Get-Member cmdlet:

$myVM.ExtensionData | Get-Member setCustomValue

Enjoy

View solution in original post

0 Kudos
7 Replies
mattboren
Expert
Expert
Jump to solution

Hello, marc045-

Once you have the VM object, you should be able to change/update an existing custom value by using the .Net View object's method, "setCustomValue".  You use this method as such:

$myVM.ExtensionData.setCustomValue("myField", "sweet new value")

The method takes two (2) strings:  the key name, and the value to set for that key.  You can find this info by browsing the vSphere API reference at http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html, or by using the Get-Member cmdlet:

$myVM.ExtensionData | Get-Member setCustomValue

Enjoy

0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

I use set-annotion normally.

Here is a quick sample script, setting annotations based on a csv file:

$vms = import-csv C:\Annotations\VMslatest.csv

foreach ($vm in $vms)
    {
    write-host $vm.VM -fore green
    If ($vm.Storage -ne ""){Get-VM $VM.VM | Set-Annotation -CustomAttribute "Storage" -Value $VM.Storage; write-host "Storage" -fore yellow}
    If ($vm.Notes){set-vm $vm.vm -Description $vm.notes -Confirm:$false; write-host "Notes" -fore yellow}
    }
One day I will virtualise myself . . .
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

how to put a timestamp like HH:MM DD/MM/YYYY into the annotation field ? is it possible

/* Please feel free to provide any comments or input you may have. */
0 Kudos
mattboren
Expert
Expert
Jump to solution

AlbertWT's question was a follow-up to the thread at http://communities.vmware.com/message/1789792#1789792.  Answer to the question about the date formatting posted in that original thread.

0 Kudos
atothek
Contributor
Contributor
Jump to solution

Thanks!

0 Kudos
apuckett
Contributor
Contributor
Jump to solution

Hi Marc,

Have you considered setting the "notes" field within the annotations?  Reason being is that this is searchable by vSphere, makes finding specific VMs easier.

If this is the case, I created a script that modifies the "notes" field by taking a string ($note), can be multiple strings concatenated or any other data as you wish, and using this line to set the "note" via the VM object ($vm).

Set-VM -VM $vm -Description $note -Confirm:$false;

In your case, it would be:

Set-VM -VM $myVM -Description $myValue -Confirm:$false;

For the complete script, have a look at my previous thread, if you haven't already.

http://communities.vmware.com/thread/320481?tstart=0

Cheers,

- Arin Puckett

0 Kudos
apuckett
Contributor
Contributor
Jump to solution

Albert,

Just use the Get-Date PowerShell cmdlet.  For example:

$date = Get-Date;

$note = $data+"`n"+$date

Set-VM -VM $vm -Description $note -Confirm:$false;

Where $data is a string of anything of importance you want in the "notes" field.

Hope that helps,

- Arin Puckett

0 Kudos