Hello,
I want to delete some VM with a VCO script, I would not use the Action element named destroyVM. I explain you why I would not use it, because in my script I make this action :
1. I list all VM into a specific folder that the name is different of ServerName-Today Date and are powered Off.
2. I would destroy each VM into this list.
But When I would just test the following command :
System.getModule("com.vmware.library.vc.vm").destroyVm("VMName");
I receive the following error message :
TypeError: Cannot read property "runtime" from undefined (Dynamic Script Module name : destroyVm#52993)
I search on google and VMware blog, I find nothing to help me !!!!
If someone have an idea, you are welcomed to respond me.
Thank you by advance
Have a nice day
Best Regards
Christophe
Hi,
if you have access to vm.name or vm.runtime, than you have access to the vmobject.
Therefore you do not have to search for it again. I'm confused...
1.) What is folderObject?
2.) Check your if-condition. It's not "&" what you want, but "&&"
3.) getAllVMsMatchingRegexp() returns an array of vmobjects, not a single vmobject. VM names are NOT unique!
maybe a small example can help:
var vmArray = VcPlugin.getAllVirtualMachines();
for (var vmIndex in vmArray)
{
if ((vmArray[vmIndex].runtime.powerState == VcVirtualMachinePowerState.poweredOff) && (vmArray[vmIndex].name == "SCT-Test-01_12_2013"))
{
System.getModule("com.vmware.library.vc.vm").destroyVm(vmArray[vmIndex]);
}
}
Hi Christophe,
you have to call the destroyVm action by providing the VirtualMachine object not only its name as string representation.
System.getModule("com.vmware.library.vc.vm").destroyVm(vmobject);
Regards
Hello virtualsabi,
Thank you for your response.
But how I can convert my VMName to a vmobject ?
Thank you
Regards
Christophe
you can use the action getAllVMsMatchingRegexp and pass in the text name. It will return an array of VMs matching the text.
If you do not want to use the action you can use the below, where regexp is the name of the VM.
matchingVMs = System.getModule("com.vmware.library.vc.vm").getAllVMsMatchingRegexp(regexp) ;
Hello Jalapeno420,
Thank you for your help, but when I make like you write I have always the same error message.
Here is my script :
for (i in folderObject){
var vm = folderObject[i];
if ((vm.runtime.powerState.value =="poweredOff") & (vm.name =="SCT-Test-01_12_2013")){
vmtd = System.getModule("com.vmware.library.vc.vm").getAllVMsMatchingRegexp(vm.name);
System.getModule("com.vmware.library.vc.vm").destroyVm(vmtd);
}
}
Here is the error :
[2014-01-27 13:31:55.147] [I] TypeError: Cannot read property "runtime" from undefined (Dynamic Script Module name : destroyVm#52993)
If you have another idea, I am happy to read you.
Thank you
Have a nice day
Best Regards
Christophe
Hi,
if you have access to vm.name or vm.runtime, than you have access to the vmobject.
Therefore you do not have to search for it again. I'm confused...
1.) What is folderObject?
2.) Check your if-condition. It's not "&" what you want, but "&&"
3.) getAllVMsMatchingRegexp() returns an array of vmobjects, not a single vmobject. VM names are NOT unique!
maybe a small example can help:
var vmArray = VcPlugin.getAllVirtualMachines();
for (var vmIndex in vmArray)
{
if ((vmArray[vmIndex].runtime.powerState == VcVirtualMachinePowerState.poweredOff) && (vmArray[vmIndex].name == "SCT-Test-01_12_2013"))
{
System.getModule("com.vmware.library.vc.vm").destroyVm(vmArray[vmIndex]);
}
}
if you want to list all the VMs of a particular folder try this, where "folder" is a VmFolder object. This does not recurse in to the sub folders.
for (i in folder.childEntity) {
if (folder.childEntity[i].vimType == "VirtualMachine") {
vm = folder.childEntity[i];
//... Do whatever you want to the VM here ...
}
}
Hello,
Thank you very much, for your help.
Now my script work fine with your help.
FYI : Here is my script (If someone would used it).
var ActualDate = new Date();
var Day = ActualDate.getDate();
var Month = ActualDate.getMonth()+1;
var Year = ActualDate.getFullYear();
var folderObject = System.getModule("com.vmware.library.vc.folder").getAllVirtualMachinesByFolder(vmFolder);
// Add 0 in front of day and month before 10
if (Day < 10) {
Day = "0"+Day;
}
if (Month < 10) {
Month = "0"+Month;
}
// List all VM into a specific folder and delete old cloned
for (vmIndex in folderObject){
var vm = folderObject[vmIndex];
if ((vm.runtime.powerState.value =="poweredOff") & (vm.name != "SCT-Test-"+Day+"_"+Month+"_"+Year) & (vm.name.substring(0,9) =="SCT-Test-")){
System.log("VM powered off : " + vm.name + " VM to be deleted : " + vm.name);
System.getModule("com.vmware.library.vc.vm").destroyVm(vm);
}
}
Have a nice day
Best Regards
Christophe