VMware {code} Community
beaunewcomb
Contributor
Contributor
Jump to solution

viPerl: List specific custom attributes for specific VM

I'd like to be able to just pass a VM name, and custom attribute name to a function and have perl return the value...Anyone have a script for this?

0 Kudos
1 Solution

Accepted Solutions
stumpr
Virtuoso
Virtuoso
Jump to solution

So I wrote this pretty quickly but basically does what you were looking for. You have to sort of match an index value (key) between the customValue[] properties and the CustomFieldsManager object's list of custom fields.

#!/usr/bin/perl -w

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	vmname => {
	type => "=s",
	variable => "VMNAME",
	help => "Name of virtual machine.",
	required => 1,
	},
	
	customfield => {
	type => "=s",
	variable => "CUSTOMFIELD",
	help => "Name of a custom field to retrieve a value from.",
	require => 1,
	},
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

Util::connect();

my ($vm_view, $vm_name, $custom_field, $customFieldsMgr, $sc);

$vm_name = Opts::get_option("vmname");
$custom_field = Opts::get_option("customfield");

$vm_view = Vim::find_entity_view( 
	view_type => "VirtualMachine",
	filter => { 'name' => $vm_name },
	properties => \[ 'name', 'summary' ], # Remove the '\' before '[' here, forum mangles it without an escape.
);

unless ( defined $vm_view ) {
	die "Virtual Machine, '$vm_name', not found.\n";
}

$sc = Vim::get_service_content();
$customFieldsMgr = Vim::get_view( mo_ref => $sc->customFieldsManager );

unless ( (defined $vm_view->summary) && defined($vm_view->summary->customValue) ) {
	print "No custom values defined for virtual machine, '$vm_name'.\n";
}
else {
	# Get the field key value from the supplied custom field name
	my $field_key = undef;
	if ( defined $customFieldsMgr->field ) {
		foreach (@{$customFieldsMgr->field}) {
			if ( $_->name eq $custom_field ) {
				$field_key = $_->key;
			}
		}
	}

	unless ( defined $field_key ) {
		die "No custom field named '$custom_field' found.\n";
	}

	my ($value, $key);
	foreach ( @{$vm_view->summary->customValue} ) {
		$key = $_->key;
		$value = $_->value;
	
		if ( $key eq $field_key ) {
			print "Virtual Machine: $vm_name\n";
			print "   $custom_field = $value\n";
		}
	}
}

Util::disconnect();

Edit: Check for the comment to remove a backslack before the '[' character, the forum mangles it if I don't put it there.

Reuben Stump | http://www.virtuin.com | @ReubenStump

View solution in original post

0 Kudos
2 Replies
stumpr
Virtuoso
Virtuoso
Jump to solution

So I wrote this pretty quickly but basically does what you were looking for. You have to sort of match an index value (key) between the customValue[] properties and the CustomFieldsManager object's list of custom fields.

#!/usr/bin/perl -w

use strict;
use warnings;

use VMware::VIRuntime;

my %opts = (
	vmname => {
	type => "=s",
	variable => "VMNAME",
	help => "Name of virtual machine.",
	required => 1,
	},
	
	customfield => {
	type => "=s",
	variable => "CUSTOMFIELD",
	help => "Name of a custom field to retrieve a value from.",
	require => 1,
	},
);

Opts::add_options(%opts);
Opts::parse();
Opts::validate();

Util::connect();

my ($vm_view, $vm_name, $custom_field, $customFieldsMgr, $sc);

$vm_name = Opts::get_option("vmname");
$custom_field = Opts::get_option("customfield");

$vm_view = Vim::find_entity_view( 
	view_type => "VirtualMachine",
	filter => { 'name' => $vm_name },
	properties => \[ 'name', 'summary' ], # Remove the '\' before '[' here, forum mangles it without an escape.
);

unless ( defined $vm_view ) {
	die "Virtual Machine, '$vm_name', not found.\n";
}

$sc = Vim::get_service_content();
$customFieldsMgr = Vim::get_view( mo_ref => $sc->customFieldsManager );

unless ( (defined $vm_view->summary) && defined($vm_view->summary->customValue) ) {
	print "No custom values defined for virtual machine, '$vm_name'.\n";
}
else {
	# Get the field key value from the supplied custom field name
	my $field_key = undef;
	if ( defined $customFieldsMgr->field ) {
		foreach (@{$customFieldsMgr->field}) {
			if ( $_->name eq $custom_field ) {
				$field_key = $_->key;
			}
		}
	}

	unless ( defined $field_key ) {
		die "No custom field named '$custom_field' found.\n";
	}

	my ($value, $key);
	foreach ( @{$vm_view->summary->customValue} ) {
		$key = $_->key;
		$value = $_->value;
	
		if ( $key eq $field_key ) {
			print "Virtual Machine: $vm_name\n";
			print "   $custom_field = $value\n";
		}
	}
}

Util::disconnect();

Edit: Check for the comment to remove a backslack before the '[' character, the forum mangles it if I don't put it there.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
lamw
Community Manager
Community Manager
Jump to solution

Created an entry at Code Central:

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

William Lam

VMware vExpert 2009

VMware ESX/ESXi scripts and resources at:

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

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