VMware Cloud Community
ymichalak
Hot Shot
Hot Shot
Jump to solution

[vR0 7.5]- Create scheduled task on vCenter for Virtual Machine Object

Hi everyone,

we try to create a vCenter scheduled task (capture below) on a Virtual Machine from vRO :

pastedImage_0.png

So that, we will use this object :

VcScheduledTaskManager.createObjectScheduledTask(VcManagedObject,VcScheduledTaskSpec);

to find "VcManagedObject" we do that like this :

// INPUT

// vcVirtual Machine

// Find the managed entity on which the action is performed.

managedObjectReference = vcVirtualMachine.moref;

System.debug("managedObjectReference found : " + managedObjectReference + " for the Virtual Machine : " + vcVirtualMachine.name);

now to create "VcScheduledTaskSpec" we need this :

pastedImage_6.png

so, in vRO :

// INPUT

// emailRequester

// Define the scheduled task specification.

scheduledTaskSpec = new VcScheduledTaskSpec();

scheduledTaskSpec.name = "Restart Virtual Machine";

scheduledTaskSpec.description = "Restart Virtual Machine Scheduled - Created from OneCloud Portal";

scheduledTaskSpec.enabled = true;

scheduledTaskSpec.scheduler= TaskScheduler;

scheduledTaskSpec.action = vcAction;

scheduledTaskSpec.notification = emailRequester;

System.debug("scheduledTaskSpec found : " + scheduledTaskSpec);

To create the "TaskScheduler" :

// INPUT

// taskRunAt - date

// taskExpireTime - date

// Create a OnceTaskScheduler and set the time to

// run the Task Action at in the Scheduler.

TaskScheduler = new VcOnceTaskScheduler();

TaskScheduler.activeTime = taskRunAt;

TaskScheduler.expireTime = taskExpireTime;

TaskScheduler.runAt = taskRunAt;

System.debug("TaskScheduler found : " + TaskScheduler);

now we need to have a VcAction and we don't undesrtand how to build this object............. :smileyangry:

If you have an idea or if you know how to build a vcAction, we take Smiley Wink

Thx for your help.

1 Solution

Accepted Solutions
ymichalak
Hot Shot
Hot Shot
Jump to solution

Ok I understood the general idea.... When we run the "Developer Center" on vSphere 6.5 wee see this line :

pastedImage_2.png

and we didn't understand the function .... Now we understood it's the same of this :

//Create Service Instance object for vCenter so we can find the scheduled task manager 

sdkConnection = vcVirtualMachine.sdkConnection; 

serviceInstanceReference = new VcManagedObjectReference(); 

serviceInstanceReference.type = "ServiceInstance"; 

serviceInstanceReference.value = "ServiceInstance"; 

serviceInstance = VcPlugin.convertToVimManagedObject(sdkConnection, serviceInstanceReference); 

// Get the serviceContent value from the serviceInstance VcServiceInstance object

serviceContent = serviceInstance.content; 

The Final Code :

// INPUT  

// vcVirtualMachine - vc:vcVirtualMachine 

// taskRunAt - Date 

// emailRequester - string 

// ATTRIRIBUT

// scheduledTaskSpecDescription - String - "Restart Guest OS - Task created from OneCloud Portal."

// scheduledTaskSpecName - String - " - Restart Guest OS"

//Create Service Instance object for vCenter so we can find the scheduled task manager 

sdkConnection = vcVirtualMachine.sdkConnection; 

serviceInstanceReference = new VcManagedObjectReference(); 

serviceInstanceReference.type = "ServiceInstance"; 

serviceInstanceReference.value = "ServiceInstance"; 

serviceInstance = VcPlugin.convertToVimManagedObject(sdkConnection, serviceInstanceReference); 

// Get the serviceContent value from the serviceInstance VcServiceInstance object

serviceContent = serviceInstance.content; 

// Build the schedule task 

onceTaskScheduler = new VcOnceTaskScheduler(); 

onceTaskScheduler.RunAt = taskRunAt; 

System.warn("onceTaskScheduler found : " + onceTaskScheduler + " for the Virtual Machine : " + vcVirtualMachine.name); 

// System.warn("onceTaskScheduler RunAt : " + onceTaskScheduler.RunAt + " for the Virtual Machine : " + vcVirtualMachine.name); 

 

// Build the Action task 

actionTask = new VcMethodAction(); 

actionTask.name = "RebootGuest";   

actionTask.argument =[]; 

// System.warn("actionTask found : " + actionTask + " for the Virtual Machine : " + vcVirtualMachine.name); 

 

// Build the scheduler Task specs 

scheduledTaskSpec = new VcScheduledTaskSpec(); 

scheduledTaskSpec.name = vcVirtualMachine.name + scheduledTaskSpecName; 

scheduledTaskSpec.description = scheduledTaskSpecDescription; 

scheduledTaskSpec.enabled = true; 

scheduledTaskSpec.scheduler = onceTaskScheduler; 

scheduledTaskSpec.action = actionTask; 

scheduledTaskSpec.notification = emailRequester; 

System.warn("scheduledTaskSpec found : " + scheduledTaskSpec + " for the Virtual Machine : " + vcVirtualMachine.name); 

  

// Create vCenter Schedule Task Manager 

vcScheduledTaskManager = serviceContent.scheduledTaskManager;  

//Create vCenter Scheduled Task 

vcScheduledTaskManager.createScheduledTask(vcVirtualMachine,scheduledTaskSpec); 

Thx so much Hejahida82​ for your help..... highly-appreciated Smiley Wink

View solution in original post

Reply
0 Kudos
11 Replies
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Hi ymichalak

To create the VcAction you should be able to use:

scheduledTaskSpec.action = new VcMethodAction(); 

Then you need to define the name of the action to perform like this:

scheduledTaskSpec.action.name = "ResetVM_Task";

I believe the ResetVM_Task will perform a guest os reboot or a reset of the VM, however make sure to test it first.

Reference for this information is a combination of Onyx tool and an old PowerCLI blog article which lists the task names, which matched back to what I got from Onyx when using Reset VM as my task type. Scheduled Tasks - MethodAction - LucD notes

Reply
0 Kudos
ymichalak
Hot Shot
Hot Shot
Jump to solution

Ok thx Hejahida82 ..... We will try tomorrow Smiley Wink

Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Try using the action name RebootGuest instead of the ResetVM_Task. That will hopefully do a graceful reboot of the VM rather than a forced power cycle. Smiley Happy

ymichalak
Hot Shot
Hot Shot
Jump to solution

Ok strange issue when we call the : createScheduledTask.

Below the code :

// INPUT

// vcVirtualMachine - vc:vcVirtualMachine

// taskRunAt - Date

// emailRequester - string

// Find the managed entity on which the action is performed.

managedObjectReference = vcVirtualMachine.moref;

managedObjectReferenceType = managedObjectReference.type;

managedObjectReferenceValue = managedObjectReference.value;

System.debug("managedObjectReference found : " + managedObjectReference + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceType found : " + managedObjectReferenceType + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceValue found : " + managedObjectReferenceValue + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the managed entity on which the action is performed.

machineEntity = new VcManagedObjectReference() ;

machineEntity.Type = managedObjectReferenceType;

machineEntity.Value = managedObjectReferenceValue;

// Build the schedule task

onceTaskScheduler = new VcOnceTaskScheduler();

onceTaskScheduler.RunAt = taskRunAt;

// Build the Action task

actionTask = new VcMethodAction();

actionTask.name = "RebootGuest"; 

// Build the scheduler Task specs

scheduledTaskSpec = new VcScheduledTaskSpec();

scheduledTaskSpec.name = 'insa021 - Restart Guest OS';

scheduledTaskSpec.description = 'Restart Guest OS - Task created from OneCloud Portal.';

scheduledTaskSpec.enabled = true;

scheduledTaskSpec.scheduler = onceTaskScheduler;

scheduledTaskSpec.action = actionTask;

scheduledTaskSpec.notification = emailRequester;

// Create vCenter Schedule Task.

vcScheduledTaskManager = new VcScheduledTaskManager() ;

vcScheduledTaskManager.createScheduledTask(machineEntity,scheduledTaskSpec);

and workflow debug :

[2019-12-10 11:17:22.112] [D]  task name : VMware vSphere Update Manager Update Download

[2019-12-10 11:17:22.113] [D]  task name : lxa076 - Restart Guest OS

[2019-12-10 11:17:22.114] [D]  task name : insa021 - Restart Guest OS

[2019-12-10 11:17:22.121] [D]  task name : VMware vSphere Update Manager Check Notification

[2019-12-10 11:17:22.122] [D] managedObjectReference found : DynamicWrapper (Instance) : [VcManagedObjectReference]-[class com.vmware.vim.binding.vmodl.ManagedObjectReference] -- VALUE : ManagedObjectReference: type = VirtualMachine, value = vm-1642, serverGuid = null for the Virtual Machine : insa022

[2019-12-10 11:17:22.124] [D] managedObjectReferenceType found : VirtualMachine for the Virtual Machine : insa022

[2019-12-10 11:17:22.125] [D] managedObjectReferenceValue found : vm-1642 for the Virtual Machine : insa022

[2019-12-10 11:17:22.126] [E] Error in (Workflow:YMK - Schedule Task - Restart Guest OS / Scriptable task (item1)#46) Cannot convert ManagedObjectReference: type = VirtualMachine, value = vm-1642, serverGuid = null to com.vmware.o11n.sdk.modeldriven.ModelWrapper

[2019-12-10 11:17:22.134] [E] Workflow execution stack:

***

item: 'YMK - Schedule Task - Restart Guest OS/item1', state: 'failed', business state: 'null', exception: 'Cannot convert ManagedObjectReference: type = VirtualMachine, value = vm-1642, serverGuid = null to com.vmware.o11n.sdk.modeldriven.ModelWrapper (Workflow:YMK - Schedule Task - Restart Guest OS / Scriptable task (item1)#46)'

workflow: 'YMK - Schedule Task - Restart Guest OS' (f311ed15-3f76-4d60-8fac-033dfec50c6b)

|  'input': name=vcVirtualMachine type=VC:VirtualMachine value=dunes://service.dunes.ch/CustomSDKObject?id='vce0421.ehc.adp.com%2Cid:vm-1642'&dunesName='VC:VirtualMachine'​

Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Try using the VC:VirtualMachine object as the machineEntity object when creating the scheduled task rather than a VcManagedObjectReference object. So use vcVirtualMachine parameter value instead of machineEntity parameter value.

Reply
0 Kudos
ymichalak
Hot Shot
Hot Shot
Jump to solution

Hello,

we have the same problem when we use VC:VirtualMachine object as the machineEntity when we create the scheduled task :

CODE :

// INPUT

// vcVirtualMachine - vc:vcVirtualMachine

// taskRunAt - Date

// emailRequester - string

// Find the managed entity on which the action is performed.

managedObjectReference = vcVirtualMachine.moref;

managedObjectReferenceType = managedObjectReference.type;

managedObjectReferenceValue = managedObjectReference.value;

managedObjectReferenceServerGuid = managedObjectReference.serverGuid;

System.debug("managedObjectReference found : " + managedObjectReference + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceType found : " + managedObjectReferenceType + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceValue found : " + managedObjectReferenceValue + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceServerGuid found : " + managedObjectReferenceServerGuid + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the managed entity on which the action is performed.

machineEntity = new VcManagedObjectReference() ;

machineEntity.type = managedObjectReferenceType;

machineEntity.value = managedObjectReferenceValue;

System.debug("machineEntity found : " + machineEntity + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the schedule task

onceTaskScheduler = new VcOnceTaskScheduler();

onceTaskScheduler.RunAt = taskRunAt;

System.debug("onceTaskScheduler found : " + onceTaskScheduler + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the Action task

actionTask = new VcMethodAction();

actionTask.name = "RebootGuest"; 

System.debug("actionTask found : " + actionTask + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the scheduler Task specs

scheduledTaskSpec = new VcScheduledTaskSpec();

scheduledTaskSpec.name = 'insa021 - Restart Guest OS';

scheduledTaskSpec.description = 'Restart Guest OS - Task created from OneCloud Portal.';

scheduledTaskSpec.enabled = true;

scheduledTaskSpec.scheduler = onceTaskScheduler;

scheduledTaskSpec.action = actionTask;

scheduledTaskSpec.notification = emailRequester;

System.debug("scheduledTaskSpec found : " + scheduledTaskSpec + " for the Virtual Machine : " + vcVirtualMachine.name);

// Create vCenter Schedule Task.

vcScheduledTaskManager = new VcScheduledTaskManager() ;

vcScheduledTaskManager.createScheduledTask(vcVirtualMachine,scheduledTaskSpec);

Workflow log :

[2019-12-11 11:50:11.389] [D] managedObjectReference found : DynamicWrapper (Instance) : [VcManagedObjectReference]-[class com.vmware.vim.binding.vmodl.ManagedObjectReference] -- VALUE : ManagedObjectReference: type = VirtualMachine, value = vm-1641, serverGuid = null for the Virtual Machine : insa021

[2019-12-11 11:50:11.390] [D] managedObjectReferenceType found : VirtualMachine for the Virtual Machine : insa021

[2019-12-11 11:50:11.393] [D] managedObjectReferenceValue found : vm-1641 for the Virtual Machine : insa021

[2019-12-11 11:50:11.394] [D] managedObjectReferenceServerGuid found : null for the Virtual Machine : insa021

[2019-12-11 11:50:11.399] [D] machineEntity found : DynamicWrapper (Instance) : [VcManagedObjectReference]-[class com.vmware.vim.binding.vmodl.ManagedObjectReference] -- VALUE : ManagedObjectReference: type = VirtualMachine, value = vm-1641, serverGuid = null for the Virtual Machine : insa021

[2019-12-11 11:50:11.402] [D] onceTaskScheduler found : DynamicWrapper (Instance) : [VcOnceTaskScheduler]-[class com.vmware.o11n.plugin.vsphere_gen.OnceTaskScheduler_Wrapper] -- VALUE : (vim.scheduler.OnceTaskScheduler) {

   dynamicType = null,

   dynamicProperty = null,

   activeTime = null,

   expireTime = null,

   runAt = java.util.GregorianCalendar[time=1576074545000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=345,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=29,SECOND=5,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]

} for the Virtual Machine : insa021

[2019-12-11 11:50:11.404] [D] actionTask found : DynamicWrapper (Instance) : [VcMethodAction]-[class com.vmware.o11n.plugin.vsphere_gen.MethodAction_Wrapper] -- VALUE : (vim.action.MethodAction) {

   dynamicType = null,

   dynamicProperty = null,

   name = RebootGuest,

   argument = null

} for the Virtual Machine : insa021

[2019-12-11 11:50:11.407] [D] scheduledTaskSpec found : DynamicWrapper (Instance) : [VcScheduledTaskSpec]-[class com.vmware.o11n.plugin.vsphere_gen.ScheduledTaskSpec_Wrapper] -- VALUE : (vim.scheduler.ScheduledTaskSpec) {

   dynamicType = null,

   dynamicProperty = null,

   name = insa021 - Restart Guest OS,

   description = Restart Guest OS - Task created from OneCloud Portal.,

   enabled = true,

   scheduler = (vim.scheduler.OnceTaskScheduler) {

      dynamicType = null,

      dynamicProperty = null,

      activeTime = null,

      expireTime = null,

      runAt = java.util.GregorianCalendar[time=1576074545000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=2,DAY_OF_MONTH=11,DAY_OF_YEAR=345,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=29,SECOND=5,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]

   },

   action = (vim.action.MethodAction) {

      dynamicType = null,

      dynamicProperty = null,

      name = RebootGuest,

      argument = null

   },

   notification = yann.michalak@adp.com

} for the Virtual Machine : insa021

[2019-12-11 11:50:11.409] [E] Error in (Workflow:YMK - Schedule Task - Restart Guest OS - DEBUG / Scriptable task (item1)#57) java.lang.NullPointerException

[2019-12-11 11:50:11.417] [E] Workflow execution stack:

***

item: 'YMK - Schedule Task - Restart Guest OS - DEBUG/item1', state: 'failed', business state: 'null', exception: 'java.lang.NullPointerException (Workflow:YMK - Schedule Task - Restart Guest OS - DEBUG / Scriptable task (item1)#57)'

workflow: 'YMK - Schedule Task - Restart Guest OS - DEBUG' (c5a4f8c4-6304-486b-8cf0-491886494f0b)

Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

The error message being returned this time is different. It is a null pointer exception which generally means you don't have a value set for a parameter you are trying to perform an action on. The log shows the last message printed is the "onceTaskScheduler Found" logging line 30 in the code snippet you posted. The error message reports a null pointer exception on line 57 of your code, the snippet only contains 49 lines though so I can't trace it back to an exact location. In your vRO client find line 57 of the code and check the contents of that line and line 56 to see if you are calling a parameter which might not have a value set. Try printing the value of the parameters to the log to check the value assigned to them.

Reply
0 Kudos
ymichalak
Hot Shot
Hot Shot
Jump to solution

Ok now back with the same error :

[2019-12-11 14:45:47.190] [E] Error in (Workflow:YMK - Schedule Task - Restart Guest OS - DEBUG / Scriptable task (item1)#49) Cannot convert ManagedObjectReference: type = VirtualMachine, value = vm-1501, serverGuid = null to com.vmware.o11n.sdk.modeldriven.ModelWrapper

[2019-12-11 14:45:47.200] [E] Workflow execution stack:

***

item: 'YMK - Schedule Task - Restart Guest OS - DEBUG/item1', state: 'failed', business state: 'null', exception: 'Cannot convert ManagedObjectReference: type = VirtualMachine, value = vm-1501, serverGuid = null to com.vmware.o11n.sdk.modeldriven.ModelWrapper (Workflow:YMK - Schedule Task - Restart Guest OS - DEBUG / Scriptable task (item1)#49)'

workflow: 'YMK - Schedule Task - Restart Guest OS - DEBUG' (c5a4f8c4-6304-486b-8cf0-491886494f0b)

with this code :

// INPUT

// vcVirtualMachine - vc:vcVirtualMachine

// taskRunAt - Date

// emailRequester - string

// Find the managed entity on which the action is performed.

managedObjectReference = vcVirtualMachine.moref;

managedObjectReferenceType = managedObjectReference.type;

managedObjectReferenceValue = managedObjectReference.value;

managedObjectReferenceServerGuid = managedObjectReference.serverGuid;

System.debug("managedObjectReference found : " + managedObjectReference + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceType found : " + managedObjectReferenceType + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceValue found : " + managedObjectReferenceValue + " for the Virtual Machine : " + vcVirtualMachine.name);

System.debug("managedObjectReferenceServerGuid found : " + managedObjectReferenceServerGuid + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the managed entity on which the action is performed.

machineEntity = new VcManagedObjectReference() ;

machineEntity.type = managedObjectReferenceType;

machineEntity.value = managedObjectReferenceValue;

System.warn("machineEntity found : " + machineEntity + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the schedule task

onceTaskScheduler = new VcOnceTaskScheduler();

onceTaskScheduler.RunAt = taskRunAt;

System.warn("onceTaskScheduler found : " + onceTaskScheduler + " for the Virtual Machine : " + vcVirtualMachine.name);

System.warn("onceTaskScheduler RunAt : " + onceTaskScheduler.RunAt + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the Action task

actionTask = new VcMethodAction();

actionTask.name = "RebootGuest"; 

actionTask.argument =[];

System.warn("actionTask found : " + actionTask + " for the Virtual Machine : " + vcVirtualMachine.name);

// Build the scheduler Task specs

scheduledTaskSpec = new VcScheduledTaskSpec();

scheduledTaskSpec.name = 'insa021 - Restart Guest OS';

scheduledTaskSpec.description = 'Restart Guest OS - Task created from OneCloud Portal.';

scheduledTaskSpec.enabled = true;

scheduledTaskSpec.scheduler = onceTaskScheduler;

scheduledTaskSpec.action = actionTask;

scheduledTaskSpec.notification = emailRequester;

System.warn("scheduledTaskSpec found : " + scheduledTaskSpec + " for the Virtual Machine : " + vcVirtualMachine.name);

// Create vCenter Schedule Task.

vcScheduledTaskManager = new VcScheduledTaskManager() ;

vcScheduledTaskManager.createScheduledTask(machineEntity,scheduledTaskSpec);

Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

Ok I know the issue and think I have a fix, just testing it now. If it works then I will add a new post with the updated code asap.

Reply
0 Kudos
Hejahida82
VMware Employee
VMware Employee
Jump to solution

The problem with the code I suggested and you have been running is that the line

vcScheduledTaskManager = new VcScheduledTaskManager() ;

does not create a valid task manager object to then create a scheduled task against. This is why it was returning a java null pointer exception.

Unfortunately there is not a method that I could find to retrieve the VcScheduledTaskManager object from the VM or vCenter directly which actually returned a value. I did find that you can create the object though with a few lines of code.

So delete the above line in your code and then add this first section to create an instance of VcServiceInstance object.

//Create Service Instance object for vCenter so we can find the scheduled task manager

var sdkConnection = vcVirtualMachine.sdkConnection;

var serviceInstanceReference = new VcManagedObjectReference();

serviceInstanceReference.type = "ServiceInstance";

serviceInstanceReference.value = "ServiceInstance";

var serviceInstance = VcPlugin.convertToVimManagedObject(sdkConnection, serviceInstanceReference);

Then get the serviceContent value from the serviceInstance VcServiceInstance object you created using the previous code snippet:

var serviceContent = serviceInstance.content;

Now create the VcScheduledTaskManager instance from the VcServiceContent object you have just created:

// Create vCenter Schedule Task Manager

var vcScheduledTaskManager = serviceContent.scheduledTaskManager;

And now finally create your scheduled task on this new VcScheduledTaskManager object.

//Create vCenter Scheduled Task

vcScheduledTaskManager.createScheduledTask(vcVirtualMachine,scheduledTaskSpec);

Make sure you are using the VC:VirtualMachine object and not the VM Entity object when creating the task, then you should not receive an error about converting the object types when you use the VC:VirtualMachine object.

That should create the scheduled task on the VM with a one time schedule using the date you have specified. Let me know if it works in your environment.

ymichalak
Hot Shot
Hot Shot
Jump to solution

Ok I understood the general idea.... When we run the "Developer Center" on vSphere 6.5 wee see this line :

pastedImage_2.png

and we didn't understand the function .... Now we understood it's the same of this :

//Create Service Instance object for vCenter so we can find the scheduled task manager 

sdkConnection = vcVirtualMachine.sdkConnection; 

serviceInstanceReference = new VcManagedObjectReference(); 

serviceInstanceReference.type = "ServiceInstance"; 

serviceInstanceReference.value = "ServiceInstance"; 

serviceInstance = VcPlugin.convertToVimManagedObject(sdkConnection, serviceInstanceReference); 

// Get the serviceContent value from the serviceInstance VcServiceInstance object

serviceContent = serviceInstance.content; 

The Final Code :

// INPUT  

// vcVirtualMachine - vc:vcVirtualMachine 

// taskRunAt - Date 

// emailRequester - string 

// ATTRIRIBUT

// scheduledTaskSpecDescription - String - "Restart Guest OS - Task created from OneCloud Portal."

// scheduledTaskSpecName - String - " - Restart Guest OS"

//Create Service Instance object for vCenter so we can find the scheduled task manager 

sdkConnection = vcVirtualMachine.sdkConnection; 

serviceInstanceReference = new VcManagedObjectReference(); 

serviceInstanceReference.type = "ServiceInstance"; 

serviceInstanceReference.value = "ServiceInstance"; 

serviceInstance = VcPlugin.convertToVimManagedObject(sdkConnection, serviceInstanceReference); 

// Get the serviceContent value from the serviceInstance VcServiceInstance object

serviceContent = serviceInstance.content; 

// Build the schedule task 

onceTaskScheduler = new VcOnceTaskScheduler(); 

onceTaskScheduler.RunAt = taskRunAt; 

System.warn("onceTaskScheduler found : " + onceTaskScheduler + " for the Virtual Machine : " + vcVirtualMachine.name); 

// System.warn("onceTaskScheduler RunAt : " + onceTaskScheduler.RunAt + " for the Virtual Machine : " + vcVirtualMachine.name); 

 

// Build the Action task 

actionTask = new VcMethodAction(); 

actionTask.name = "RebootGuest";   

actionTask.argument =[]; 

// System.warn("actionTask found : " + actionTask + " for the Virtual Machine : " + vcVirtualMachine.name); 

 

// Build the scheduler Task specs 

scheduledTaskSpec = new VcScheduledTaskSpec(); 

scheduledTaskSpec.name = vcVirtualMachine.name + scheduledTaskSpecName; 

scheduledTaskSpec.description = scheduledTaskSpecDescription; 

scheduledTaskSpec.enabled = true; 

scheduledTaskSpec.scheduler = onceTaskScheduler; 

scheduledTaskSpec.action = actionTask; 

scheduledTaskSpec.notification = emailRequester; 

System.warn("scheduledTaskSpec found : " + scheduledTaskSpec + " for the Virtual Machine : " + vcVirtualMachine.name); 

  

// Create vCenter Schedule Task Manager 

vcScheduledTaskManager = serviceContent.scheduledTaskManager;  

//Create vCenter Scheduled Task 

vcScheduledTaskManager.createScheduledTask(vcVirtualMachine,scheduledTaskSpec); 

Thx so much Hejahida82​ for your help..... highly-appreciated Smiley Wink

Reply
0 Kudos