VMware Cloud Community
herb_daheim
Contributor
Contributor

Read Custom Field under c#

I'm looking for a solution how to read out a custom field of a virtual machine.

I found VirtualMachine.setCustomAttribute(key,value), but I miss the corresponding getCustomAttribute(key,value) method.

I found several fields returning the key only, but I do not have read access to the values even I can set them.

Any idea how the trick is, because when I look into the PowerShell API this functionality seems to exist?

I'm using PowerCLI 5.5 under C#.

Thanks for help in adavance

0 Kudos
4 Replies
LucD
Leadership
Leadership

I can't give you the C# code, but I can give you the prototype in PowerShell.

$vmName = "MyVM"
$fieldName = "FIELD"

$custMgr = Get-View CustomFieldsManager
$tgtField = $custMgr.Field | where {$_.Name -eq $fieldName}

# Retrieve the value
$vm = Get-View -ViewType VirtualMachine -Property Name,CustomValue -Filter @{"Name"=$vmName}
$vm.CustomValue | where {$_.Key -eq $tgtField.Key} | Select -ExpandProperty Value

# Set the value
$custMgr.SetField($vm.ExtensionData.MoRef,$tgtField.Key,"Test")


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
herb_daheim
Contributor
Contributor

Thanks, but this is exactly my problem:

in C# the property VirtualMachine.CustomValue contains only the Key property, but the Value property is not available.

But since PS knows how to read there must be a solution to solve this problem nevertheless.

0 Kudos
LucD
Leadership
Leadership

If you look at the base class ExtensibleManagedObject you'll notice that there are 2 properties, availableField and value.

The PowerCLI .net mapping is slightly different, but you will have to run through the value entries and find the one with the correct customattribute key.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gislig
Contributor
Contributor

I´m trying to read CustomField Attributes using VMWare.VIM from C#

Here is my solution, you can use this for anything

0 Kudos