VMware {code} Community
brrSCO
Contributor
Contributor
Jump to solution

CustomFieldsManager object not working

I have setup Custom Values for VM guests using the VC Client GUI. I am using the VI Perl Toolkit to try to retrieve those values in code. I have found code snippets that describe how to do this but mine fail. I have traced the line that is failing and it appears my CustomFieldsManager is not working at all in VI. When I run the following short bit of code from a Perl script:

use strict;

use warnings;

use Getopt::Long;

use VMware::VIRuntime;

my $username = "username";

my $password = "password";

my $service_url = "[https://servername/sdk/vimService]";

Vim::login(service_url => $service_url, user_name => $username, password => $password);

my $csi_view;

my $si_moref = ManagedObjectReference->new(type => 'ServiceInstance',value=>'ServiceInstance');

my $si_view = Vim::get_view(mo_ref => $si_moref);

my $sc_view = $si_view->RetrieveServiceContent();

my $cfm_view = Vim::get_view(mo_ref => $sc_view->customFieldsManager);

The last line is causing the error below:

Can't call method "type" on an undefined value at /usr/share/perl/5.8/VMware/VICommon.pm line 828.

When I look in my MOB for this ESX Host, and RetrieveServiceContent, the CustomFieldsManager object shows "Unset" next to it, even though I have Custom Fields created and filled in for the VM's on this host. How do I use this CustomFields functionality from the VI Perl toolkit?

Reply
0 Kudos
1 Solution

Accepted Solutions
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

First let me give you a brief of the CustomFieldsManager object. This object is used to managed the Custom fields that one defines let say by using the VI Client connected to VC. The important point here is that these custom fields implementation is only available through VC (since the VC uses a database to store this information) and not on the ESX directly. Therefore all the information regarding the Custom Fields would only be available through the Virtual Center. Try running your script on the VC and then you should be able to get the desired results.

Hope the above information helps.

~ Sidharth

View solution in original post

Reply
0 Kudos
7 Replies
ssurana
VMware Employee
VMware Employee
Jump to solution

Hi,

First let me give you a brief of the CustomFieldsManager object. This object is used to managed the Custom fields that one defines let say by using the VI Client connected to VC. The important point here is that these custom fields implementation is only available through VC (since the VC uses a database to store this information) and not on the ESX directly. Therefore all the information regarding the Custom Fields would only be available through the Virtual Center. Try running your script on the VC and then you should be able to get the desired results.

Hope the above information helps.

~ Sidharth

Reply
0 Kudos
StefanPahrmann
Jump to solution

Hi,

Try the following to show custom field names:

my $customFieldsManager = Vim::get_view(mo_ref => Vim::get_service_content()->customFieldsManager);
if (defined $customFieldsManager->{field}) 
{
        my $fields = $customFieldsManager->{field};
        foreach(@$fields) 
        {
                print         "Field name:\t" . $_->name . "\n";
        }
}

Hope this helps.

Regards

Stefan

###############

If you found this information useful, please consider awarding points for "Correct" or "Helpful". Thanks!!!

LarsOeschey
Contributor
Contributor
Jump to solution

very nice, I'm currently also looking into Custom Fields. Maybe someone could give me a hint on how to access a Custom field of a specific VM? What I want to do is get a list of all VM's in a VC and look if a specific variable (i.e. Custom field) is set and then react on that...

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Take a look at these two scripts to get you started:

=========================================================================

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

VMware Code Central - Scripts/Sample code for Developers and Administrators

VMware Developer Comuunity

Twitter: @lamw

If you find this information useful, please award points for "correct" or "helpful".

Reply
0 Kudos
LarsOeschey
Contributor
Contributor
Jump to solution

thanks, very helpful

Reply
0 Kudos
filip_hasa
Contributor
Contributor
Jump to solution

Hello, please can you repost your two scripts. There are not available anymore. Thank you very much

Reply
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Reply
0 Kudos