Hello everyone,
I need to create several scheduled task to relocate VM with Storage Migration.
The script create tasks reading infos from a csv file formatted with 2 column: VM,DestDatastore
The script is based on the one decribed in this article: http://www.lucd.info/2009/10/18/scheduled-tasks-methodaction/
Below my changes:
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$CsvFile
)
$startTime = [Datetime] $(Get-Date -format 'MM/dd/yyyy') + " 20:00"
$startInterval = 5
$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
Import-Csv $CsvFile | % {
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$_.VM}
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "svMotion_to_VMAX_of_" + $_.VM
$spec.Description = "Migrate " + $_.VM + " to " + $_.DestDatastore
$spec.Enabled = $true
$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 VMware.Vim.VirtualMachineRelocateSpec
$arg1.Value.datastore = (Get-Datastore -Name $_.DestDatastore).Extensiondata.MoRef
$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)
}
My problem is that I'm unable to code how to convert disks to thick lazy zeroed during storage migration (regardless the original format).
Any idea?
Thanks
Message was edited by: vcubegiovannivecchi Code updated
The MethodAction for a scheduled task is rather limited.
Afaik it doesn't give you access to all possibilities of the invoked method (RelocateVM in this case).
I guess it would be a lot more flexible to use the RunScriptAction for the scheduled task, and then do the migration and format change in that script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ThanksLucD for your reply.
Do you confirm that it's there is no way to script the disk format change during the scheduled task creation?
No, not afaik.
Via the Web Client/C# Client that option is also not there.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD I think you are wrong:
It can be done according to this KB:
VMware KB: Changing the thick or thin provisioning of a virtual disk
Additionally if looking at the Set-HardDisk Cmdlet, it looks like it is possible to change formats as long as the datastore is also changing:
LucD it seems that RunScriptAction could be triggered only by an alarm...
I stand corrected :smileygrin:
Let me check that out, and see how they pass that parameter on the CreateScheduledTask call.
We are still talking about a Scheduled Task ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this.
It will specify the format per vDisk connected to the VM
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$CsvFile
)
$startTime = [Datetime] $(Get-Date -format 'MM/dd/yyyy') + " 20:00"
$startInterval = 5
$si = get-view ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$offset = 0
Import-Csv $CsvFile | % {
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$_.VM}
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "svMotion_to_VMAX_of_" + $_.VM
$spec.Description = "Migrate " + $_.VM + " to " + $_.DestDatastore
$spec.Enabled = $true
$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 VMware.Vim.VirtualMachineRelocateSpec
$vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]} | %{
$disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$disk.DiskId = $_.Key
$disk.Datastore = $ds.Extensiondata.MoRef
$disk.diskBackingInfo =New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$disk.diskBackingInfo.ThinProvisioned = $false
$disk.diskBackingInfo.FileName = ''
$disk.diskBackingInfo.DiskMode = ''
$disk.diskBackingInfo.ThinProvisioned = $false
$arg1.Value.disk += $disk
}
$spec.Action.Argument += $arg1
$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference