I have been given this tricky task and i just cant figure out how to put all the pieces together.
I need to create a script that will intelligently decided what datastore to deploy a vm to. We don't want to deploy to a datastore that has the most space, we want to deploy to the datastore that has the least amount of space but can still accommodate the space for the ( vm+5% ) and still leave 50GB free on the lun after the move.
So for example, if the vm is 40GB, we want the script to select the datastore that has a close to 90GB available without being under.
So my thought for this approach is:
I have a few other scripts i scrounged up on the internet that i've been trying to piece together... but i just dont quiet have all the parts...
Any help would be greatly appreciated!!!
-Knotz
Try something like this
# Get all datastores
$ds = Get-Datastore | Where-Object { ($_.Name -match "PAR0[1-4]_[edp]*disk*") -and ($_.Name -notmatch "local")} |
Select-Object Name,
@{n="CapacityGB";e={[math]::round(($_.CapacityMB/1024))}},
@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024))}},
@{n="FreeSpacePercent";e={[math]::round(($_.FreeSpaceMB/$_.CapacityMB*100))}}
foreach($vm in Get-VM){
# Find possible candidates
$candidates = $ds | where {($_.FreeSpaceGB - $vm.UsedSpaceGB - 50) -gt 0}
# Find best candidate
$target = $candidates | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1
Write-Output "VM $($vm.Name) can go to $($target.Name)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I was able to combine these to get a good list of Capacity,FreeSpace, and Free space percent on just available/deploy-able luns.
get-datastore | where-object { ($_.Name -match "PAR0[1-4]_[edp]*disk*") -and ($_.Name -notmatch "local")} | Select-Object Name,@{n="CapacityGB";e={[math]::round(($_.CapacityMB/1024))}},@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024))}},@{n="FreeSpacePercent";e={[math]::round(($_.FreeSpaceMB/$_.CapacityMB*100))}} | Sort-Object FreeSpaceGB
Now i could easily export this to a csv... but then what... <scratches head>
-Knotz
Try something like this
# Get all datastores
$ds = Get-Datastore | Where-Object { ($_.Name -match "PAR0[1-4]_[edp]*disk*") -and ($_.Name -notmatch "local")} |
Select-Object Name,
@{n="CapacityGB";e={[math]::round(($_.CapacityMB/1024))}},
@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024))}},
@{n="FreeSpacePercent";e={[math]::round(($_.FreeSpaceMB/$_.CapacityMB*100))}}
foreach($vm in Get-VM){
# Find possible candidates
$candidates = $ds | where {($_.FreeSpaceGB - $vm.UsedSpaceGB - 50) -gt 0}
# Find best candidate
$target = $candidates | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1
Write-Output "VM $($vm.Name) can go to $($target.Name)"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
After you found your target, you should start to deploy your VM on that target. Depending on how you want to deploy your VM's you should parse the target value to your deployment.
For example you could use New-VM to deploy your VM
New-VM -Name VM2 -Template Template -VMHost $host -datastore $target
Now i know its ugly, but i got this far on my own...and it seems to pull the right datastore. I still need to pull just the name as opposed to the @<Name=datastorename> naming format. anyway.. going to abandon this for a few and try your script above. looks much cleaner, like someone who knew what they were doing .
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$vmsizegb=50
$vmsizemb=($vmsizegb*1024)
$datastoreminsizemb=($vmsizemb+51000)
$datastoreminsizegb=($datastoreminsizemb/1024)
get-datastore | where-object { ($_.Name -match "PAR0[1-4]_[edp]*disk*") -and ($_.Name -notmatch "local") -and ($_.FreeSpaceMB -gt $datastoreminsizemb) } | Select-Object Name,@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024-$datastoreminsizegb))}} | Sort-Object FreeSpaceGB | Select-Object -first 1
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
So LucD,
thanks a ton for the help! i pretty much combined our ideas and came up with this.. which works beautifully!!!
at some point before this, ill figure out that $vmsize variable and assign as such and then run this part. But this was the hard part imo.
$vmsize=150
$dsminspace=($vmsize+50)
$ds = Get-Datastore | Where-Object { ($_.Name -match "PAR0[1-4]_[edp]*disk*") -and ($_.Name -notmatch "local")} |
Select-Object Name,
@{n="CapacityGB";e={[math]::round(($_.CapacityMB/1024))}},
@{n="FreeSpaceGB";e={[math]::round(($_.FreeSpaceMB/1024))}},
@{n="FreeSpacePercent";e={[math]::round(($_.FreeSpaceMB/$_.CapacityMB*100))}} |
where {($_.FreeSpaceGB - $vm.UsedSpaceGB - $dsminspace) -gt 0} | Sort-Object -Property FreeSpaceGB | Select -First 1
write-output "$($ds.Name)"
then follow that up with something like this....ill also have to figure out some host logic as well...
New-VM -Name VM2 -Template Template -VMHost $host -datastore $ds
Thanks a ton guys. mucho appreciated. +1 for vmware communities!
-Knotz