Hey,
Sorry i'm a little new to javascript and i'm trying to create a custom workflow, basically i have an action that gets all vms from a cluster, i then wanted to write a scriptable element to select the servers which are powered on an pass them as an output. I keep getting the error "cannot read property "powerState" from undefined. I have tried a few variations of code but below is the latest.
input = vmlist
output = vmpoweron
code
var vms = vmlist;
vmpoweron = (status.runtime.powerState.name == "poweredOn");
Thanks
Rich
sorry there is a typo here, should read
var vms = vmlist;
vmpoweron = (vmlist.runtime.powerState.name == "poweredOn");
Your action gets all the VMs for you and presents that as an array of vmObjects named vmlist correct?
To get the power status of the individual vm's you are going to need to for loop all the machines so it would like something like this...
var onVMs = new Array();
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn"){onVMs.push(vm)}
}
This should get you an array of poweredOn VM's
Thank you for your reply sbeaver,
I am now trying to manipulate your code to power off the vm as passing the attribute to an action workflow doesnt do anything. I obviously doing something wrong as it seems to just ignore the script able task.
var onVMs = new Array();
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn");
VcAutoStartAction.guestShutdown;
else {
throw ("Server already powered off" + vm.name + "<br>");
}
I think you want
vm.shutdownGuest();
i get this error
Error in (Workflow:shutdown guest and host / Scriptable task (item11)#7) The attempted operation cannot be performed in the current state (Powered off)
here is the current code
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn");
vm.shutdownGuest();
}
Try
if (vm.runtime.powerState.name == "poweredOn") {
vm.shutdownGuest();
}
Thank you very much that did work however it kept trying to shutdown the same VM and then failed carrying on with the rest of the workflow saying the tools were not running (obviously because the server was powered off by that point).
Sorry to be a pain but this is where i am now and it keeps failing on the first vm saying the server is already powered off but does not carry on to look at the rest, i'm pulling my hair out.
Current code
validationMessage = "The server is already powered off <br>"
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn"){
vm.shutdownGuest();
} else {
throw ("Server already powered off" + vm.name + "<br>");
system.log("Error Message: " + validationMessage);
}
}
Try it without throwing.
Ok so that worked so much better however it never moves onto the next step in the workflow, it constantly tries to shutdown the guest even after its been shut down.
Current code
validationMessage = "The server is already powered off <br>"
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn"){
vm.shutdownGuest();
} else { ("Server already powered off" + vm.name + "<br>");
}
}
Your else statement doesn't do anything. Can you change it to System.debug(yourAlreadyPoweredOffMessage); and also System.debug("shutting down " + vm.name) in the 'if poweredOn' section, and then post your logs?
It now just keeps trying to shutdown the guest and sends loads of emails
var vms = vmlist;
for each ( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn"){
vm.shutdownGuest();
System.debug("Shutting Down" + vm.name + "<br>");
} else {
System.debug("Server already powered off" + vm.name + "<br>");
}
}
[2018-05-24 10:10:57.787] [E] Error in (Workflow:shutdown guest and host / Scriptable task (item11)#5) Cannot complete operation because VMware Tools is not running in this virtual machine.
[2018-05-24 10:10:57.846] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:10:59.994] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:02.096] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:04.053] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:06.000] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:06.564] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:08.551] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:16.384] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:19.317] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:20.293] [I] sending mail to host: xxx.xxx.xxx.xxx with user:null, from:PowerDownNotification@.com, to:email@email.com
[2018-05-24 10:11:21.044] [E] Workflow execution stack:
***
item: 'shutdown guest and host/item7', state: 'failed', business state: 'null', exception: 'Exception binding empty'
workflow: 'shutdown guest and host' (ec199468-2725-458d-854e-ec1337986d37)
| 'attribute': name=vmlist type=Array/VC:VirtualMachine value=#{#VC:VirtualMachine#dunes://service.dunes.ch/CustomSDKObject?id='vcenter.com/vm-709735'&dunesName='VC:VirtualMachine'#;#VC:VirtualMachine#dunes://service.dunes.ch/CustomSDKObject?id='vcenter.com/vm-709736'&dunesName='VC:VirtualMachine'#;#VC:VirtualMachine#dunes://service.dunes.ch/CustomSDKObject?id='vcenter.com/vm-709733'&dunesName='VC:VirtualMachine'#}#
| 'attribute': name=Hosts type=Array/VC:HostSystem value=#{#VC:HostSystem#dunes://service.dunes.ch/CustomSDKObject?id='vcenter.com/host-787369'&dunesName='VC:HostSystem'#}#
| 'attribute': name=smtpHost type=string value=xxx.xxx.xxx.xxx
| 'attribute': name=smtpPort type=number value=25.0
| 'attribute': name=username type=string value=
| 'attribute': name=password type=SecureString value=__NULL__
| 'attribute': name=fromName type=string value=Shutdown Guest and Cluster
| 'attribute': name=No type=boolean value=false
| 'attribute': name=content type=string value=<b>There was an error while powering down the following guests and hosts </b> <br>nullDynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.vmo.plugin.vi4.model.VimVirtualMachine] -- VALUE : VirtualMachine<vm-709735>'Server1',DynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.vmo.plugin.vi4.model.VimVirtualMachine] -- VALUE : VirtualMachine<vm-709736>'Server1',DynamicWrapper (Instance) : [VcVirtualMachine]-[class com.vmware.vmo.plugin.vi4.model.VimVirtualMachine] -- VALUE : VirtualMachine<vm-709733>'Server1'DynamicWrapper (Instance) : [VcHostSystem]-[class com.vmware.vmo.plugin.vi4.model.VimHostSystem] -- VALUE : HostSystem<host-787369>'hostname.com'
| 'attribute': name=subject type=string value=Power Down Region Notification
| 'attribute': name=toAddress type=string value=email@email.com
| 'attribute': name=fromAddress type=string value=PowerDownNotification@email.com
| 'attribute': name=errorCode type=string value=
| 'attribute': name=waitInSeconds type=number value=__NULL__
| 'attribute': name=counter type=number value=__NULL__
| 'attribute': name=onVMs type=string value=__NULL__
| 'attribute': name=validationMessage type=string value=__NULL__
| 'input': name=cluster type=VC:ClusterComputeResource value=dunes://service.dunes.ch/CustomSDKObject?id='vcenter.com/domain-c395700'&dunesName='VC:ClusterComputeResource'
| 'attribute': name=max_iterations() type=number value=1
| 'attribute': name=_iterator type=number value=0
| 'attribute': name=eval()_ type=number value=0
| 'no outputs'
*** End of execution stack.
Got there in the end with a wait action, thanks for your help
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
var vms = vmlist;
for each( var vm in vmlist ){
if (vm.runtime.powerState.name == "poweredOn"){
vm.shutdownGuest();
wait(15000);
System.debug("Shutting Down" + vm.name + "<br>");
} else {
System.debug("Server already powered off" + vm.name + "<br>");
}
}