#!/usr/bin/perl -w # William Lam # http://blogs.vmware.com/vsphere/automation use strict; use warnings; use VMware::VILib; use VMware::VIRuntime; my %opts = ( vmname => { type => "=s", help => "Name of Virtual Machine to migrate", required => 1, }, vihost => { type => "=s", help => "Name of ESXi host to migrate to", required => 1, }, datastore => { type => "=s", help => "Name of Datastore to migrate to", required => 1, }, priority => { type => "=s", help => "Migration priority [high|low]", required => 0, default => 'high', }, ); Opts::add_options(%opts); Opts::parse(); Opts::validate(); Util::connect(); my $vmname = Opts::get_option('vmname'); my $vihost = Opts::get_option('vihost'); my $datastore = Opts::get_option('datastore'); my $priority = Opts::get_option('priority'); if(Vim::get_service_content()->about->apiVersion ne "5.1") { &seeya("Script requires vCenter Server 5.1\n"); } # define priority enums my %priorityConstants = ('high' => 'highPriority', 'low' => 'lowPriority'); # retrieve VM my $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine', filter => {'name' => $vmname}, properties => ['name']); if(!defined($vm_view)) { &seeya("Unable to find VM: " . $vmname . "\n") } # retrieve host my $host_view = Vim::find_entity_view(view_type => 'HostSystem', filter => {'name' => $vihost}, properties => ['name']); if(!defined($host_view)) { &seeya("Unable to find ESXi host: " . $vihost . "\n"); } # retrieve datastore my $datastore_view = Vim::find_entity_view(view_type => 'Datastore', filter => {'name' => $datastore}, properties => ['name']); if(!defined($datastore_view)) { &seeya("Unable to find datastore: " . $datastore . "\n"); } # in case bad input if($priority ne "low" || $priority ne "high") { $priority = "high"; } # relocate spec for both datastore, host my $spec = VirtualMachineRelocateSpec->new(datastore => $datastore_view, host => $host_view); my $migrationPriority = VirtualMachineMovePriority->new($priorityConstants{$priority}); my ($task,$message); eval { # call relocate API print "Migrating " . $vmname . " to ESXi Host: " . $vihost . " & Datastore: " . $datastore . " ...\n"; $task = $vm_view->RelocateVM_Task(spec => $spec, priority => $migrationPriority); $message = "Successfully migrated " . $vmname . "!\n"; &getStatus($task,$message); }; if($@) { print "Error: " . $@ . "\n"; } Util::disconnect(); sub getStatus { my ($taskRef,$message) = @_; my $task_view = Vim::get_view(mo_ref => $taskRef); my $taskinfo = $task_view->info->state->val; my $continue = 1; while ($continue) { my $info = $task_view->info; if ($info->state->val eq 'success') { print $message,"\n"; return $info->result; $continue = 0; } elsif ($info->state->val eq 'error') { my $soap_fault = SoapFault->new; $soap_fault->name($info->error->fault); $soap_fault->detail($info->error->fault); $soap_fault->fault_string($info->error->localizedMessage); die "$soap_fault\n"; } sleep 5; $task_view->ViewBase::update_view_data(); } } sub seeya { my ($message) = @_; print $message; Util::disconnect(); exit 1; }