vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 928 forks source link

Sending an e-mail as part of a Snapshot using the Task Scheduler #723

Open DesertRanger829 opened 1 year ago

DesertRanger829 commented 1 year ago

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 create_schedule_mail(vm) File ..\createSnapshot.py", line 118, in create_schedule_mail scheduledTask = stm.Create(vm, spec=taskSpec) File "C:\Program Files\Python39\lib\site-packages\pyVmomi\VmomiSupport.py", line 598, in self.f(*(self.args + (obj,) + args), **kwargs) File "C:\Program Files\Python39\lib\site-packages\pyVmomi\VmomiSupport.py", line 388, in _InvokeMethod return self._stub.InvokeMethod(self, info, args) File "C:\Program Files\Python39\lib\site-packages\pyVmomi\SoapAdapter.py", line 1575, in InvokeMethod raise obj # pylint: disable-msg=E0702 pyVmomi.VmomiSupport.InvalidArgument: (vmodl.fault.InvalidArgument) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A specified parameter was not correct: spec.action', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], invalidProperty = 'spec.action' }

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