I create virtual machines via a script that randomizes the datastore the guest is put on. The -Datastore parameter I am using to do this is "
-Datastore ($myds.getvalue($rnd.next(0,$myds.count)))" where $rnd is the random number generator "
$rnd = New-Object System.Random" and $myds is "$myds = Get-datastore | where {$_.Name -match "DS"}" that pertains to how a name my datastores.
The randomization works great to a point. Sometimes the guest machine is attempted to be placed in a datastore that has no more availble space. The question is, would it be best to round robin the guest machines so that they are evenly distributed. This decreases the chance that one datastore will hold so many more VM's over the others, and therefore reduce the risk of running out of space? Should I put this into a try-catch block to catch errors.
Just trying to figure out the best way to do this without having to worry about not having enough space. Error checking is obviously needed.
Thanks in advance.
Personally, I'd take free space into account, and number of VMs on the DS.
It is possible for a random number generator to get the same Datastore several times in a row, so I'd go with something that does the following
Of course if you are feeling really clever, you could interrogate perfromance stats for the past month and go with the least loaded one? - either way, random number generator is not ideal
Also, try-catch is not the way to do it, rather a manual check (intelligent decision)
get-datastore returns FreeSpaceMB, so the calculation is very simple (just extend your where statement)