pyvmomi-community-samples
pyvmomi-community-samples copied to clipboard
Sending an e-mail as part of a Snapshot using the Task Scheduler
Hi all, I have a script that uses the following function to create a scheduled task snapshot.
def create_schedule_task(vm):
spec = vim.scheduler.ScheduledTaskSpec()
spec.name = 'Snapshot vm %s' % vm.name
spec.description = ''
spec.scheduler = vim.scheduler.OnceTaskScheduler()
spec.scheduler.runAt = schedule_time
spec.action = vim.action.MethodAction(name=vim.VirtualMachine.CreateSnapshot)
spec.action.argument = [
vim.action.MethodActionArgument(value=snapshot_name),
vim.action.MethodActionArgument(value=snapshot_description),
vim.action.MethodActionArgument(value=False),
vim.action.MethodActionArgument(value=False),
]
spec.name = f"Snapshot-'{vm.name}'-{time.strftime('%Y%m%d-%H%M%S')}"
spec.enabled = True
stm = si.content.scheduledTaskManager
scheduledTask = stm.Create(vm, spec=spec)
return scheduledTask
Now I want to send an email as part of that scheduled task. But I'm not sure if I can add multiple Actions for a scheduled task. I considered creating a separate task which sends the email but I get the following error:
[...]
File "..\createSnapshot.py", line 205, in
Additional context
That is how I tried to implement the seperate task
action = vim.action.SendEmailAction()
action.body = "test"
action.ccList = ""
action.subject = subject
action.toList = to_address
runAt = schedule_time
scheduler = vim.scheduler.OnceTaskScheduler(runAt=runAt)
taskSpec = vim.scheduler.ScheduledTaskSpec(name="SendEmail-%s" % vm.name,
description="",
action=action,
scheduler=scheduler,
enabled=True)
stm = si.content.scheduledTaskManager
scheduledTask = stm.Create(vm, spec=taskSpec)
return scheduledTask