I have this script below that is still not returning the correct datastore. It returns HDS-ESXi-UCS-0403 instead of HDS-ESXi-UCS-0408 which has more free space. They are both attached to the Non-Production cluster.
When I look in vSphere I see 0403 as Capacity 2.50TB and Free as 1.10TB and I see 0408 as Capacity 2.50TB and Free as 1.75TB
bestFreeSpace = 0;
datastore = null;
dsCapacity = null;
dsFreeSpace = null;
percentFree = null;
Cluster = VcPlugin.getAllClusterComputeResources(null, "Non-Production")[0];
for(i in Cluster.datastore){
selectedDatastore = Cluster.datastore[i];
if ( selectedDatastore.name.toLowerCase().indexOf("hds-esxi-ucs-04") == 0 ) {
dsCapacity = selectedDatastore.summary.capacity / 1024 / 1024 / 1024;
dsFreeSpace = selectedDatastore.summary.freeSpace / 1024 / 1024 / 1024;
percentFree = dsFreeSpace / dsCapacity * 100;
if ( percentFree > 25 ){
if ( dsFreeSpace > bestFreeSpace ) {
bestFreeSpace = dsFreeSpace;
datastore = selectedDatastore;
}
}
}
}
if ( datastore==null ) { throw "No datastore found which has over 25% free space!"; }