Latest script:
I implemented 2 and 3.
$dstores = Import-Csv "C:\vmstuff\dstores.csv" -UseCulture
$vhosts = Import-Csv "C:\vmstuff\vhosts.csv" -UseCulture
$countvm = 0
$countds = 0
$countvh = 0
Import-Csv "C:\vmstuff\vms.csv" -UseCulture | %{
$vm = $_.vm
$vhost = ""
$vhostcap = 0
$vhostused = 0
$dstorecap = 0
$dstorefree = 0
$dsfound = $False
$error = ""
# Check for data storage availability:
for($d=0; $d -lt $dstores.Count; $d++)
{
$countds += $d
$dstore = $dstores[$countds % $dstores.Count].name
$dstorecap = (get-datastore $dstores[$countds % $dstores.Count].name).CapacityMB
$dstorefree = (get-datastore $dstores[$countds % $dstores.Count].name).FreeSpaceMB
$dstoreused = [int]$dstores[$countds % $dstores.Count].used
if ($dstorefree -gt 25000)
{
$dsfound = $True
break;
}
}
if ( $dsfound -eq $True )
{
# Check for host capacity:
$vhfound = $False
for($h=0; $h -lt $vhosts.Count; $h++)
{
$countvh += $h
$vhost = $vhosts[$countvh % $vhosts.Count].name
$vhostcap = $vhosts[$countvh % $vhosts.Count].capacity
$vhostused = [int]$vhosts[$countvh % $vhosts.Count].used
if($vhostused -lt $vhostcap)
{
$vhfound = $True
break;
}
}
if ( $vhfound -eq $True )
{
#New-VM -Name $_.vm -VMHost (Get-VMHost $host) -Datastore $store ....
$vhostused++
$vhosts[$countvh % $vhosts.Count].used = $vhostused
$dstoreused = [int]$dstores[$countds % $dstores.Count].used
$dstoreused += 25000
$dstores[$countds % $dstores.Count].used = $dstoreused
$message = "OK"
}
else
{
$message = "NOT ENOUGH POWER (!)"
}
}
else
{
$message = "NOT ENOUGH STORAGE (!)"
#$dstoreused = $dstores[$countds % $dstores.Count].used
}
write-host "$count: $vm $vhost ($vhostused/$vhostcap VMs) $dstore ($dstorefree/$dstoreused/$dstorecap MB) $message"
$countvm++
$countds++
$countvh++
}
What I will need to do is build up the New-VM cmdlet with my parameters and actually execute this on a real environment and see how it goes.
I am still missing to implement an easy way to blow up all of the existing VMs.
Any tips or feedback will be appreciated.
Thanks