Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DesertRanger829 opened this issue May 12, 2023 · 0 comments
Open

Comments

@DesertRanger829
Copy link

DesertRanger829 commented May 12, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant