VMware Cloud Community
MariusRoma
Expert
Expert

Creating a scheduled task to clone a VM

I need to create a scheduled task (more scheduled tasks...) to clone VM VM001 as VM002.

The script should not clone the VM, it rather should create a scheduled task to clone the VM.

Is there any sample I can start from?

Regards

marius

17 Replies
LucD
Leadership
Leadership

Try something like this

You will have to change the time when the scheduled task runs to your requirements.

$srcVMName = 'VM001'

$tgtVMName = 'VM002'

# Find object(s) on which to perform action

$VM = Get-VM -Name $srcVMName

$ma = New-Object VMware.Vim.MethodAction

$ma.Argument = $null

$ma.Name = "CloneVM_Task"

$ar1 = New-Object VMware.Vim.MethodActionArgument

$ar1.Value = $vm.Folder.ExtensionData.MoRef

$ma.Argument += $ar1

$ar2 = New-Object VMware.Vim.MethodActionArgument

$ar2.Value = $tgtVMName

$ma.Argument += $ar2

$ar3 = New-Object VMware.Vim.MethodActionArgument

$vmSpec = New-Object VMware.Vim.VirtualMachineCloneSpec

$vmSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec

$vmSpec.Location.Datastore = $vm.ExtensionData.Datastore[0]

$vmSpec.Location.Pool = (Get-ResourcePool -VM $VM).ExtensionData.MoRef

$vmSpec.Config = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmSPec.Template = $false

$vmSpec.PowerOn = $false

$ar3.Value = $vmSpec

$ma.Argument += $ar3

# Run at 21:00 every day

$dTScheduler = New-Object VMware.Vim.DailyTaskScheduler

$dTScheduler.Hour = 21

$dTScheduler.Minute = 0

$dTScheduler.Interval = 1

# Create the scheduled task

$tSpec = New-Object VMware.Vim.ScheduledTaskSpec

$tSpec.Action = $ma

$tSpec.Description = "Clone " + $vm.Name

$tSpec.Enabled = $TRUE

$tSpec.Name = "Clone " + $vm.Name

$tSpec.Notification = "my.email@address.com"

$tSpec.Scheduler = $dTScheduler

$stMgr = Get-View ScheduledTaskManager

$stMgr.CreateScheduledTask($vm.ExtensionData.MoRef,$tSpec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alex_OP
Contributor
Contributor

Hi LucD,

Thank you !! This was very helpful for me !!

Do you know how can I use this script for a Shutdown and start VM?

Thanks.

0 Kudos
LucD
Leadership
Leadership

Sure, try like this

$VMName = 'VM01'

# Find object(s) on which to perform action

$VM = Get-VM -Name $VMName

$ma = New-Object VMware.Vim.MethodAction

$ma.Argument = $null

$ma.Name = "PowerOnVM_Task"

# Run at 21:00 every day

$dTScheduler = New-Object VMware.Vim.DailyTaskScheduler

$dTScheduler.Hour = 21

$dTScheduler.Minute = 0

$dTScheduler.Interval = 1

# Create the scheduled task

$tSpec = New-Object VMware.Vim.ScheduledTaskSpec

$tSpec.Action = $ma

$tSpec.Description = "Poweron " + $vm.Name

$tSpec.Enabled = $TRUE

$tSpec.Name = "Poweron " + $vm.Name

$tSpec.Notification = "my.email@address.com"

$tSpec.Scheduler = $dTScheduler

$stMgr = Get-View ScheduledTaskManager

$stMgr.CreateScheduledTask($vm.ExtensionData.MoRef,$tSpec)

For the poweroff its the same script, just replace 'powerOn' by 'powerOff' everywhere.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alex_OP
Contributor
Contributor

Quite simply Smiley Wink

Thanks again !


And I think that, if I would create a "poweroff task" I have to replace "PowerOnVM_Task" by "PowerOffVM_Task" with the same parameters?

Sincerely

0 Kudos
LucD
Leadership
Leadership

Yes, see my note at the bottom of my previous reply


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alex_OP
Contributor
Contributor

Oops, indeed. Thanks again.

Before create my tasks, I would like search if the tasks already exist and if yes, delete them.

I found this :

(Get-View -Id ((Get-VIScheduledTasks -Full | ?{ $_.Name -eq $taskName }).ScheduledTask)).RemoveScheduledTask()

But don't work for me :smileycry:

0 Kudos
LucD
Leadership
Leadership

Change the last two line into

$stMgr = Get-View ScheduledTaskManager

# Remove any existing scheduled task with the same name

$oldTask = Get-View -id $stMgr.ScheduledTask | where{$_.Info.Name -eq $tSpec.Name}

if($oldTask){

    $oldTask.RemoveScheduledTask()

}

$stMgr.CreateScheduledTask($vm.ExtensionData.MoRef,$tSpec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Alex_OP
Contributor
Contributor

Thank you for having answered so quickly my questions !

0 Kudos
oteodoro
Contributor
Contributor

Senpai Lucy.. Can put additional line to only put - rotational 1VM ? after 1 day it will delete the old VM clone?

Objective: Scheduled Task 1 VM - rotational - delete the old version from nextday..

Thank you senpai.

0 Kudos
LucD
Leadership
Leadership

You could create a scheduled task to remove the clone, let's say at 20:55 each day.
The drawback would be that you are living 5 minutes without the clone (until 21:00).

Is that acceptable?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
oteodoro
Contributor
Contributor

Yeah...

0 Kudos
oteodoro
Contributor
Contributor

LucD, this is i upload directly to SSH VCenter is it? What extension name is this?

0 Kudos
LucD
Leadership
Leadership

You can create an additional scheduled task like this

$VMName = 'VM2'

# Find object(s) on which to perform action

$VM = Get-VM -Name $VMName


$ma = New-Object VMware.Vim.MethodAction

$ma.Argument = $null

$ma.Name = "Destroy_Task"


# Run at 20:55 every day

$dTScheduler = New-Object VMware.Vim.DailyTaskScheduler

$dTScheduler.Hour = 20

$dTScheduler.Minute = 55

$dTScheduler.Interval = 1


# Create the scheduled task

$tSpec = New-Object VMware.Vim.ScheduledTaskSpec

$tSpec.Action = $ma

$tSpec.Description = "Remove " + $vm.Name

$tSpec.Enabled = $TRUE

$tSpec.Name = "Remove " + $vm.Name

$tSpec.Notification = "my.email@address.com"

$tSpec.Scheduler = $dTScheduler


$stMgr = Get-View ScheduledTaskManager

$stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $tSpec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
oteodoro
Contributor
Contributor

Got this mate.. i used powerCLi.. i can see the task now.

0 Kudos
oteodoro
Contributor
Contributor

Ahhh oki got this.. means it will be 2 task.. thank u very much senpai.

0 Kudos
oteodoro
Contributor
Contributor

Hi Bro Lucd,

The delete script you gave is in VM level... If job has been deleted... then the next day.. the script does not exist.. Can u integrate it in host level instead?

Thank you.

0 Kudos
LucD
Leadership
Leadership

I'm afraid not.
This a Scheduled Task of the Method type.
The "method" we call (Destroy_Task) needs to be available on the object on which we create the Scheduled Task.


An alternative could be to schedule the activities on an external scheduler.
Like for example the Windows Task Scheduler.

There you could call a PS script to perform all the actions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos