VMware Cloud Community
lukeglazebrook
Enthusiast
Enthusiast

Script that calculates the disk size of of the VM’s being cloned and if they will fit on the Datastore exist's?

I have a script that clones a bunch of VM's from a text file from 1 location to a specific data store and it works great.  I need to incorporate a validation check at the start to confirm the combined VM's from the text file will fit on the datastore OK.  I'm totally stuck at the highlighted point below in bold can anyone kindly give me a clue Smiley Sad

Would be extremely grateful if someone could give me a pointer



The below is what what I have so far...

# ==============================================================================================

# Varibles

# ==============================================================================================

# Add the vmware snapin for powershell

Add-PSSnapin VMware.VimAutomation.Core

# backup = true appends date; false creates a clone with the same name.

$backup = "True"

# debug - true : will not clone vm; FALSE will clone the vm.

$debug = "FALSE"

# Target Datastore

$targetdatastore = "Synology LUN1"

#Refers to the percentage of free space for the DS space check

$freePerc = 5

# Target location - existing folder in vcenter structure, where the clones will be stored

$targetlocation = "TestTarget"

# Set date

$datestart = (get-date -uformat %Y-%m-%d)

# Name a logfile to capture results.

$logfile = $datestart + "_VMClones_bulk.txt"

write-output "New Log ($datestart) - ($logfile)" >> $logfile

# Establish Connection

Connect-VIServer -Server 127.0.0.1 -User administrator@topsecret-Password topsecret

# ==============================================================================================

# Validation checks Prior to starting the clone script

# ==============================================================================================

$ds = Get-Datastore -Name $targetdatastore

# Validation check 1, Does the combined provisioned total in GB from $VmListFile exceed the capacity of the destination datastore?

if(($ds.FreeSpaceGB -lt $VmListFile.  Combined size of $VmListFile                            )

            {

                        Write-Host "Continue with your script"

            }

else

            {

                        Write-Host "There is insufficient space on the target datastore. Please increase Space to continue"

                        Write-Output "There is insufficient space on the target datastore. Please increase Space to continue" >> $logfile

                        Write-Host "Exiting with code 1"

                        Write-Output "Exiting with code 1" >> $logfile

                        exit 1

            }

$ds = Get-Datastore -Name $targetdatastore

# Validation check 2, do you have X % free datatstorespace?

if(($ds.FreeSpaceGB/$ds.CapacityGB*100) -ge $freePerc)

            {

                        Write-Host "Continue with your script"

            }

else

            {

                        Write-Host "There is less than X% space free on the datastore. Please increase Space to continue"

                        Write-Output "There is less than X% space free on the datastore. Please increase Space to continue" >> $logfile

                        Write-Host "Exiting with code 2"

                        Write-Output "Exiting with code 2" >> $logfile

                        exit 2

            }

$VmListFile = Get-Content VmsToCloneList.list | Select-Object -Skip 3

ForEach ($vmname in $VmListFile)

            {

                        # The VM Object

                        $vm = Get-VM -Name $vmname

                        # Target Host - use the same host as the current VM ( this is faster than cloning across hosts ).

                        $targethost = $vm.vmhost.name

                        # Target VM Name - name if BACKUP is FALSE

                        $vmtarget = $vmname

                        #

                        $datastore = get-datastore $targetdatastore -vmhost $targethost

# ==============================================================================================

# Begin the actual cloning script

# ==============================================================================================

                        if ($vm -ne $null)

                                    {

                                                Write-Host "A VM named $VM exists. Starting clone"

                                                Write-output "A VM named $VM exists. Starting clone"  >> $logfile

                                    }

                        else

                                    {

                                                Write-Host "A VM by the name of $VM perhaps it does not exists"

                                                Write-Output "A VM by the name of $VM perhaps it does not exists" >> $logfile

                                                Write-Host "Exiting with code 2"

                                                Write-Output "Exiting with code 2" >> $logfile

                                                exit 2

                                    }

                        if ($backup -eq "TRUE")

                                    {

                                                # Clone the VM to backup_vmname_todaysdate

                                                $vmtarget = "backup_" + $vmtarget + "_" + $datestart

                                    }

                        # nice colors if you are watching the script run

                        write-host -foregroundcolor green "Cloning $vm to $vmtarget"

                        write-output -foregroundcolor green "Cloning $vm to $vmtarget" >> $logfile

                        new-vm -name $vmtarget -vm $vm -vmhost $targethost -datastore $datastore -Location $targetlocation -DiskStorageFormat thin

            }

# COMPLETED


0 Kudos
4 Replies
LucD
Leadership
Leadership

Let me see if I understood the question.

You want to calculate $VmListFile ?

Is that the size of the VM on the current row in the input file, or the combined size of all the VMs in the input file ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
lukeglazebrook
Enthusiast
Enthusiast

Its a combined size of several VM's mate which complicates things a tad.  I came up with this but unsurprisingly it doesn't work Smiley Sad

$VmListFile = Get-Content VmsToCloneList.list

$ds = Get-Datastore -Name $targetdatastore

# Validation check 1, Does the combined provisioned usage of the VM's exceed the target destination datastore?

if(($ds.FreeSpaceGB -lt $VmListFile.UsedSpaceGB))

            {

                        Write-Host "Continue with your script"

  }

0 Kudos
lukeglazebrook
Enthusiast
Enthusiast

combined size of all the VMs in the input file basically

0 Kudos
Craig_Baltzer
Expert
Expert

So if your input file is just a list of VMs such as

VM1

VM3

VM9

VM27

then you could do

$vmnames = Get-Content VMnames.txt

$TotVMUsed=0

(Get-VM -Name $vmnames).ExtensionData.Guest.Disk | ForEach { $TotVMUsed += $_.Capacity - $_.FreeSpace }

and then in your "If" use $TotVMUsed as the "in use size" of the VM. Note that this only includes the size of the VMDK files, so you should likely add a bit of "fudge factor" for the VMX file, etc. It also assumes that all the VMDKs of the VM reside on the same datastore

The other consideration is if you have swap on the same datastore as the VM you may be able to clone successfully but be unable to start all the VMs as there is insufficient space for swap; if you want to accommodate that you'd need to factor in RAM size - reservation (plus a bit of "fudge")

0 Kudos