<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get/Set VM Custom Attributes in vSphere SDK for Perl Discussions</title>
    <link>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358444#M245</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Certainly!  The only trick is having to enumerate the Custom Fields from the CustomFieldsManager to get the &lt;STRONG&gt;key&lt;/STRONG&gt; property you need to call &lt;STRONG&gt;SetField&lt;/STRONG&gt; on.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's some working code you can start with.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="code"&gt;
#!/usr/bin/perl

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	vmname =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "vmname",
	help =&amp;gt; "Virtual Machine Name",
	required =&amp;gt; 1,
	},
	
	fieldname =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "fieldname",
	help =&amp;gt; "Custom Field Name",
	required =&amp;gt; 1,
	},
	
	fieldvalue =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "fieldvalue",
	help =&amp;gt; "CustomFieldValue",
	required =&amp;gt; 1,
	},
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

Util::connect();

my $vm_name = Opts::get_option("vmname");
my $field_name = Opts::get_option("fieldname");
my $field_value = Opts::get_option("fieldvalue");

my $vm_view = Vim::find_entity_view(view_type =&amp;gt; "VirtualMachine",
									filter =&amp;gt; { 'name' =&amp;gt; $vm_name});
									
unless ($vm_view) {
		Util::disconnect();
		die "Failed to find VM by name: $vm_name\n";
}

my $CustomFieldsManager = Vim::get_view( mo_ref =&amp;gt; Vim::get_service_content()-&amp;gt;customFieldsManager );

my $field_key = FindCustomFieldKey($CustomFieldsManager, $field_name);

unless ($field_key) {
	my $new_custom_field = CreateVMFieldDefinition($CustomFieldsManager, $field_name);
	$field_key = $new_custom_field-&amp;gt;key;
} 

SetVMFieldValue($CustomFieldsManager, $vm_view, $field_key, $field_value);
	
sub FindCustomFieldKey {
	
	my ($cfm, $field_name) = @_;
	my ($field_key, @custom_fields);
	if ($cfm-&amp;gt;field) {
		@custom_fields = @{$cfm-&amp;gt;field};
	}
	else {
		# No custom fields defined, return undef
		return undef;
	}
	
	foreach my $field ( @custom_fields ) {
		if ($field-&amp;gt;name eq $field_name) {
			$field_key = $field-&amp;gt;key;
			last;
		}
	}
	
	# will be undef if no matching field to $field_name is found
	return $field_key;
}
	
sub CreateVMFieldDefinition {
	
	my ($cfm, $field_name) = @_;
	
	print "Adding Custom Field \'$field_name\'...\n";
	my $new_cf = $cfm-&amp;gt;AddCustomFieldDef(
		name =&amp;gt; $field_name,
		moType =&amp;gt; "VirtualMachine",
		fieldDefPolicy =&amp;gt; undef,
		fieldPolicy =&amp;gt; undef);
		
	return $new_cf;
}

sub SetVMFieldValue {
	
	my ($cfm, $entity, $field_key, $field_value) = @_;
	
	print "Setting Custom Field for Virtual Machine...\n";
	$cfm-&amp;gt;SetField(
		entity =&amp;gt; $entity,
		key =&amp;gt; $field_key,
		value =&amp;gt; $field_value );
		
	return;
}
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 12 Apr 2010 20:44:58 GMT</pubDate>
    <dc:creator>stumpr</dc:creator>
    <dc:date>2010-04-12T20:44:58Z</dc:date>
    <item>
      <title>Get/Set VM Custom Attributes</title>
      <link>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358443#M244</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Grettings!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As you already know, it is possible to define custom attributes for a virtual machine from the vm's summary tab. It is also possible to define global attibutes to all inventory vms...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any chances of getting/setting these "attributes" from perl SDK?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Apr 2010 19:18:33 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358443#M244</guid>
      <dc:creator>martelera</dc:creator>
      <dc:date>2010-04-12T19:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Get/Set VM Custom Attributes</title>
      <link>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358444#M245</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Certainly!  The only trick is having to enumerate the Custom Fields from the CustomFieldsManager to get the &lt;STRONG&gt;key&lt;/STRONG&gt; property you need to call &lt;STRONG&gt;SetField&lt;/STRONG&gt; on.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's some working code you can start with.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="code"&gt;
#!/usr/bin/perl

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	vmname =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "vmname",
	help =&amp;gt; "Virtual Machine Name",
	required =&amp;gt; 1,
	},
	
	fieldname =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "fieldname",
	help =&amp;gt; "Custom Field Name",
	required =&amp;gt; 1,
	},
	
	fieldvalue =&amp;gt; {
	type =&amp;gt; "=s",
	variable =&amp;gt; "fieldvalue",
	help =&amp;gt; "CustomFieldValue",
	required =&amp;gt; 1,
	},
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

Util::connect();

my $vm_name = Opts::get_option("vmname");
my $field_name = Opts::get_option("fieldname");
my $field_value = Opts::get_option("fieldvalue");

my $vm_view = Vim::find_entity_view(view_type =&amp;gt; "VirtualMachine",
									filter =&amp;gt; { 'name' =&amp;gt; $vm_name});
									
unless ($vm_view) {
		Util::disconnect();
		die "Failed to find VM by name: $vm_name\n";
}

my $CustomFieldsManager = Vim::get_view( mo_ref =&amp;gt; Vim::get_service_content()-&amp;gt;customFieldsManager );

my $field_key = FindCustomFieldKey($CustomFieldsManager, $field_name);

unless ($field_key) {
	my $new_custom_field = CreateVMFieldDefinition($CustomFieldsManager, $field_name);
	$field_key = $new_custom_field-&amp;gt;key;
} 

SetVMFieldValue($CustomFieldsManager, $vm_view, $field_key, $field_value);
	
sub FindCustomFieldKey {
	
	my ($cfm, $field_name) = @_;
	my ($field_key, @custom_fields);
	if ($cfm-&amp;gt;field) {
		@custom_fields = @{$cfm-&amp;gt;field};
	}
	else {
		# No custom fields defined, return undef
		return undef;
	}
	
	foreach my $field ( @custom_fields ) {
		if ($field-&amp;gt;name eq $field_name) {
			$field_key = $field-&amp;gt;key;
			last;
		}
	}
	
	# will be undef if no matching field to $field_name is found
	return $field_key;
}
	
sub CreateVMFieldDefinition {
	
	my ($cfm, $field_name) = @_;
	
	print "Adding Custom Field \'$field_name\'...\n";
	my $new_cf = $cfm-&amp;gt;AddCustomFieldDef(
		name =&amp;gt; $field_name,
		moType =&amp;gt; "VirtualMachine",
		fieldDefPolicy =&amp;gt; undef,
		fieldPolicy =&amp;gt; undef);
		
	return $new_cf;
}

sub SetVMFieldValue {
	
	my ($cfm, $entity, $field_key, $field_value) = @_;
	
	print "Setting Custom Field for Virtual Machine...\n";
	$cfm-&amp;gt;SetField(
		entity =&amp;gt; $entity,
		key =&amp;gt; $field_key,
		value =&amp;gt; $field_value );
		
	return;
}
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 12 Apr 2010 20:44:58 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358444#M245</guid>
      <dc:creator>stumpr</dc:creator>
      <dc:date>2010-04-12T20:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: Get/Set VM Custom Attributes</title>
      <link>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358445#M246</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;AWESOME ! ! ! :smileygrin:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you so much stumpr!!     &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 13 Apr 2010 11:39:54 GMT</pubDate>
      <guid>https://communities.vmware.com/t5/vSphere-SDK-for-Perl-Discussions/Get-Set-VM-Custom-Attributes/m-p/358445#M246</guid>
      <dc:creator>martelera</dc:creator>
      <dc:date>2010-04-13T11:39:54Z</dc:date>
    </item>
  </channel>
</rss>

