MariusRoma
Expert
Expert

VMware.Vim.VirtualMachineRelocateSpec clarification

Jump to solution

I try to run a script to create scheduled tasks to move VMs from one datastore to a different one.

I use the VMware.Vim.VirtualMachineRelocateSpec object to specify where each VM must be relocated.

I want to change only the deatastore, not the host, but while creating the scheduled tast I cannot specify "Change datastore" because "Change both host and datastore" is selected anyway.

This is a problem if the VM is powered on...

Can anybody please help?

Regards

marius

1 Solution

Accepted Solutions
vThinkBeyondVM
VMware Employee
VMware Employee

As per official API documentation@ VMware.Vim.VirtualMachineRelocateSpec ,

We can just specify target datastore, hence any vSphere SDK + PowerCLI also must honor it.

Can you please post your script here?

If it is useful, plz mark answer as correct or helpful.
----------------------------------------------------------------
Thanks & Regards
Vikas, VCP50, MCTS on AD, SCJP6.0.
http://vThinkBeyondVM.com
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

View solution in original post

5 Replies
vThinkBeyondVM
VMware Employee
VMware Employee

As per official API documentation@ VMware.Vim.VirtualMachineRelocateSpec ,

We can just specify target datastore, hence any vSphere SDK + PowerCLI also must honor it.

Can you please post your script here?

If it is useful, plz mark answer as correct or helpful.
----------------------------------------------------------------
Thanks & Regards
Vikas, VCP50, MCTS on AD, SCJP6.0.
http://vThinkBeyondVM.com
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

aaron416
Enthusiast
Enthusiast

Per the documentation, you don't have to specify a host when setting up the specification. Thus, leaving the host property unset should work as a valid relocation spec.

MariusRoma
Expert
Expert

In include my script, based on what suggested by LucD while answering to my prevuous post Creating scheduled tasks

#

# Based on da http://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/

#

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer <IP Address> -User <username> 

$csvName = "E:\Test.csv"

$tgtDatastore = <target datastore name>

$emailAddr = <my e-mail address>

$startTime = [Datetime]"10/21/2016 20:00" # late in the future, just to see if the script works

$startInterval = 5

$si = get-view ServiceInstance

$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

$offset = 0

Import-Csv $csvName | % {

$a = $_.VMname

$a

$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$_.VMname}

$spec = New-Object VMware.Vim.ScheduledTaskSpec

$spec.Name = "svMotion " + $_.VMname

$spec.Description = "Migrate " + $_.VMname + " to " + $tgtDatastore

$spec.Enabled = $true

$spec.Notification = $emailAddr

$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler

$spec.Scheduler.runat = $startTime.AddMinutes($offset)

$offset += $startInterval

$spec.Action = New-Object VMware.Vim.MethodAction

$spec.Action.Name = "RelocateVM_Task"

$arg1 = New-Object VMware.Vim.MethodActionArgument

$arg1.Value = New-Object

$arg1.Value.datastore = (Get-Datastore $tgtDatastore | Get-View).MoRef

$arg1.Value.pool = $vm.ResourcePool

$arg1.Value.host = $vm.Runtime.Host

$arg1.Value.host

$spec.Action.Argument += $arg1

$arg2 = New-Object VMware.Vim.MethodActionArgument

$arg2.Value = [VMware.Vim.VirtualMachineMovePriority]"defaultPriority"

$spec.Action.Argument += $arg2

$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)

}


The scheduled task creation is fine, but the task is based on changing both host and datastore.

How can I change the settings?

Regards

marius



0 Kudos
MariusRoma
Expert
Expert

I made some further tests and I discovered that:

  • If I don't specify any host and any pool and the VM is powered on, a task to change the datastore is created
  • If I don't specify any host and any pool and the VM is powered off, a task to change either host and datastore is created

Is there any way to specify that I want only to change the datastore because my VM is powered on?

Regards

marius

0 Kudos
MariusRoma
Expert
Expert

I made a further test in a lab environment and I noticed that the tasks created with the script work fine, even if they look to be set in the wrong way.

I apologize for dubiting...

Many thanks to LucD.

Regards

marius

0 Kudos