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
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
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
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}
}
how to put a timestamp like HH:MM DD/MM/YYYY into the annotation field ? is it possible
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.
Thanks!
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
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