Created
June 13, 2013 22:33
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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