Skip to content

Instantly share code, notes, and snippets.

Created June 13, 2013 22:33
Show Gist options
  • Select an option

  • Save anonymous/5777983 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5777983 to your computer and use it in GitHub Desktop.
Sample vSphere SDK for Perl script showing how to build up a Managed Object using MoRef ID
#!/usr/bin/perl -w
# William Lam
# http://www.virtuallyghetto.com/
use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;
my %opts = (
vmname => {
type => "=s",
help => "Name of VM",
required => 1,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();
my $vmname = Opts::get_option('vmname');
# searches for VM object based on vmname input
my $vm = Vim::find_entity_view(view_type => 'VirtualMachine', properties => ['name'], filter => {'name' => $vmname});
if(defined($vm)) {
# performs simple async task, save the Task Ref
my $taskRef = $vm->PowerOnVM_Task();
# get Task Ref view
my $taskView = Vim::get_view(mo_ref => $taskRef);
# get Task MoRef ID
my $taskMoRefId = $taskView->{'mo_ref'}->value;
# print out MoRef ID + state of task
print "MoRefID: " . $taskMoRefId . "\t " . "State:" . $taskView->info->state->val . "\n";
# adding a dummer timer, you could do something else and then check back later using MoRef ID
sleep 10;
# build a new MO using the Task MoRef ID
my $newTaskRef = ManagedObjectReference->new(type => "Task", value => $taskMoRefId);
# get Task Ref view
my $newTaskView = Vim::get_view(mo_ref => $newTaskRef);
# get Task MoRef ID from new object to ensure it's the same one
my $newTaskMoRefId = $taskView->{'mo_ref'}->value;
# print Task MoRef ID + state and you should see it change
print "MoRefID: " . $newTaskMoRefId . "\t " . "State:" . $newTaskView->info->state->val . "\n";
# power off VM
$vm->PowerOffVM_Task();
} else {
print "Unable to find $vmname\n";
}
Util::disconnect();
### SAMPLE OUTPUT ###
lamw:~/vmware » ./taskExample.pl --server mini --username root --vmname VM1
MoRefID: haTask-20-vim.VirtualMachine.powerOn-506061389 State:running
MoRefID: haTask-20-vim.VirtualMachine.powerOn-506061389 State:success
@shawncplus
Copy link

So any ideas why get_view doesn't return it but TaskHistoryCollector does?

@shawncplus
Copy link

The tl;dr is I have a task key and need to get the TaskInfo for it. It looks like the get_view approach is the correct way but it's not working so if I could either A) get it working or B) Figure out why it's not returning the task I would be stoked

@lamw
Copy link

lamw commented Jun 13, 2013

FYI - It does work for me. I don't have a chatty system right now, but I just did a self-vMotion which looks like the type of task you had originally. Then looked at the latestTask property and used "task-102" and it worked for me.

@shawncplus
Copy link

http://www.youtube.com/watch?v=krD4hdGvGHM I initiated two new tasks (powerOff and powerOn) and after they finished I hit /task/task-(task-id) (Endpoint that runs the get_view code) and both worked. However the older tasks in the list of tasks for that machine (Tasks returned from TaskCollector) did not work. I wonder if there's some TTL on get_view or something odd. Either way, at least now I can handle the Fault and and know the code is right.

@shawncplus
Copy link

Workflow:

Initiate task on Machine A

/task/task-(new task id)

Hooray, get stuff

Initiate new task on Machine A

/task/task-(new task id)

Hooray, get stuff

/task/task-(old task id)

Boooo, task is gone, despite still showing up in the list of tasks for that machine with full details and with the same task id as it originally had. Nothing changed about the task as far as I can tell except that get_view won't return it

However,

Initiate task on Machine A

/task/task-id get stuff

Initiate Task on Machine B

/task/task-id get stuff
/task/task-old-task-id get stuff

tl;dr I think get_view can only return the most recent task (FOR A SINGLE MACHINE). That seems.... very, very wrong and/or broken.

EDIT: Gah! This doesn't seem to be the case either.

EDIT2: Seems to be time related, it stopped being returned from get_view around 5-7 minutes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment