VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
1 Replies Last post: May 31, 2009 9:51 PM by Seemankij  

create a schedule task of poweroff the specified Virtual Machine using the SDK getting failed posted: May 28, 2009 11:57 AM

Click to view kumar_sudhir1's profile Novice 19 posts since
Mar 30, 2006

Hi everyone,

I made a code to create a schedule task using the SDK, but I have errors:

Here is my code:

// Find the virtual machine list from the virtual machine folder found above.

ManagedObjectReference] vmList = (ManagedObjectReference[)getObjectProperty(vmFolderRef, "childEntity");

//Searching for the template/virtual machine

ManagedObjectReference templateRef = null;

for (int i = 0; i < vmList.Length; i++)

{

if (vmList.type == "VirtualMachine")

{

Object[] vmProps = getProperties(vmListi], new String[ { "config.name", "config.template" });

if (((String)vmProps[0]).Equals(srcName))

{

templateRef = vmList;

}

}

}

// Define the operation to be performed.

MethodAction ma = new MethodAction();

ma.argument = new MethodActionArgument[1];

ma.name = "PowerOffVM_Task";

DateTime theDate = DateTime.Now;

theDate.AddMinutes(1);

// Define the scheduled task specification.

OnceTaskScheduler dTScheduler = new OnceTaskScheduler();

dTScheduler.runAt = theDate;

dTScheduler.runAtSpecified = true;

dTScheduler.activeTimeSpecified = true;

dTScheduler.activeTime = DateTime.Now;

dTScheduler.expireTime = theDate.AddHours(1);

dTScheduler.expireTimeSpecified = true;

ScheduledTaskSpec tSpec = new ScheduledTaskSpec();

tSpec.description = "Revert to the specified snapshot according to the schedule.";

tSpec.enabled = true;

tSpec.name = "Power Off Virtual Machine";

tSpec.action = ma;

tSpec.scheduler = dTScheduler;

tSpec.notification = Foo@Bar.com;

VS.CreateScheduledTask(_ServiceContent.scheduledTaskManager, templateRef, tSpec);

Error: SOAPEXCEPTION was unhandled. Aspecified parameter was not correct.

Do i am doing wrong with this parameter:--------------------------------------------

ma.argument = new MethodActionArgument[1];

Regards

Sudhir Kumar

Click to view Seemankij's profile Expert 261 posts since
Sep 30, 2007
Please check the method action you created and also if the Virtual Machine MORef is correct or not. Refer following code snippet for MethodAction

MethodAction action = new MethodAction();
action.name = "PowerOffVM_Task";
action.argument= new MethodActionArgument[] { };

DateTime currentTime = new DateTime();
currentTime = DateTime.Now;
DateTime runTime = currentTime.AddMinutes(30);

OnceTaskScheduler scheduler = new OnceTaskScheduler();
scheduler.runAt= runTime;

String taskName = "PowerOff a Virtual machine";
ScheduledTaskSpec scheduleSpec = new ScheduledTaskSpec();
scheduleSpec.name=taskName;
scheduleSpec.description="PowerOff VM in 30 minutes";
scheduleSpec.enabled=true;

scheduleSpec.action=taskAction;
scheduleSpec.scheduler=scheduler;

if(_virtualMachine != null) {
ManagedObjectReference task =
_service.CreateScheduledTask(
_scheduleManager, _virtualMachine, scheduleSpec);
}

Developer Social Media

Communities