mercredi 27 novembre 2019

powercli create snapshot if vm's datastore has more than %10 free space

Hi i'm workin over a script for days. I want to take vm snasphot if vm's datastore has requred space.

My script :

$myarray =@{}
$myarray = get-vm test | get-datastore | select-object @{N="DSFreespace"; E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}}
$Treshold = "{0:n2}" -f 10

foreach ($Treshold in $myarray) {
if ($myarray -ge $Treshold){new-snapshot -vm test -name test123} else {
Write-Host "You cannot take a snapshot. Datastore free spce is lower than 10%" }
}

When i run script in shell it Also i composed another one for same thing, but no luck. when i use "-ge" condition, script always take snapshot of vm, regardless free space percent (i tried it many different numbers, other than original treshold)

İf i use "-gt" condition, script never take snaphot, regardless free space percent.

I also tried another script for same thin, same results. Also, same thing for -lt and -le conditions

$vm = get-vm test
$Treshold = "{0:n2}" -f 10
$DSFreespace = get-vm $vm| get-datastore | select-object @{N="DSFreespace"; E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}}
if($DSFreespace -ge $Treshold){new-snapshot -vm $vm -name test123} else {
Write-Host "You cannot take a snapshot. Datastore free space is lower than 10%" }'''

What is wrong, how to solve this problem ?

Aucun commentaire:

Enregistrer un commentaire