I'm trying to get a list of ESX hosts from cluster and check the connection state of the host eg; maintenance mode, connected, disconnected etc. I was able to get the list of ESX hosts from cluster using getallhostsystemsofcluster(cluster). How do I get the state of host using script. Any ideas ....
Try looking at VcHostRuntimeInfo, there will find the objects "connectionState" and "inMaintenanceMode" amongst others.
An example (taking a cluster as input):
for (i in Cluster.host) {
selectedHost=Cluster.host[i];
System.log("Host: "+selectedHost.name);
System.log("Power State: "+selectedHost.runtime.powerState.value);
System.log("In Maintenance?: "+selectedHost.runtime.inMaintenanceMode);
if (selectedHost.runtime.powerState.value == "poweredOn" && selectedHost.runtime.inMaintenanceMode == false) {
//do some action based on this scenario
}
//If you want to find the number of runningVms on the cluster (for some reason or other)
runningVms = 0;for(j in Cluster.host[i].vm){if(Cluster.host[i].vm[j].runtime.powerState.name=="poweredOn"){runningVms++;}}System.log("Total number of running hosts: "+runningVms);}
Like this:
var connectionState = host.runtime.connectionState.value;
where "host" is your VC:HostSystem object.
Thanks for your quick response.
Thanks and Regards
Sundar Sankaranarayanan
The one liner below returns "Connected" for all ESX hosts. one of the ESX host in cluster is in maintenance mode and still it returns "Connected", is there a way for the one liner to return the actual state of the ESX host i.e if it is in maintenance mode, it should return as maintenance mode instead of connected.
Try looking at VcHostRuntimeInfo, there will find the objects "connectionState" and "inMaintenanceMode" amongst others.
An example (taking a cluster as input):
for (i in Cluster.host) {
selectedHost=Cluster.host[i];
System.log("Host: "+selectedHost.name);
System.log("Power State: "+selectedHost.runtime.powerState.value);
System.log("In Maintenance?: "+selectedHost.runtime.inMaintenanceMode);
if (selectedHost.runtime.powerState.value == "poweredOn" && selectedHost.runtime.inMaintenanceMode == false) {
//do some action based on this scenario
}
//If you want to find the number of runningVms on the cluster (for some reason or other)
runningVms = 0;for(j in Cluster.host[i].vm){if(Cluster.host[i].vm[j].runtime.powerState.name=="poweredOn"){runningVms++;}}System.log("Total number of running hosts: "+runningVms);}
Oops... sorry about that, I kinda rushed that in a test lab that only had a single host available
Try Dave's code, likely to get better results...
