The script runs against vCenter Server, retrieves each VM, parses the data and sends the needed information to a CSV file. The file has the following structure: VM name, number of disks attached to the VM, used space without swap file (in GB), total used space (in GB), provisioned space (in GB).
A short explanation on the 3 different values for space:
- provisioned space is the space requested by the VM on the datastore. For thick provisioned VMs the allocated (used) space is equal to requested space. Thin provisioned ones do not receive the space unless they consume it. Hence the difference between provisioned space and used space (allocated) columns
- disk space reported in used column contains other files than the VM disks (vmdk). One of this files is the VM swap file, which is equal to the difference between the VM memory and the amount of VM reserved memory. This means that if there are no reservations on the VM memory, each VM will have the swap space equal to the size of memory. This file is not part of the backup and can be excluded. If we do a math exercise - 150 VMs with 8 GB each means more than 1 TB of data which we can ignore from calculations
- finally, looking at vm10 in the image above, we see the 2 columns (used and used without swap) being equal. Since vm10 is powered on, this means it has a memory reservation equal to the whole memory. For powered off VMs, the 2 columns will always be equal since swap file is created only for running VMs.
Let's take a look at the code. First we define a function that takes as input a VM object and returns a custom made power shell object with the required properties of the VM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function GetVMData($v) { $vmResMemory = [math]::Round($v.ExtensionData.ResourceConfig.MemoryAllocation.Reservation/1024,2) $vmMem = [math]::Round($v.MemoryMB/1024,2) $vmUsedSpace = [math]::Round($v.UsedSpaceGB,2) if ($v.PowerState -match "PoweredOn") { $vmUsedSpaceNoSwap = $vmUsedSpace - $vmMem + $vmResMemory # removing swap space from calculations } else { $vmUsedSpaceNoSwap = $vmUsedSpace } $vmProvSpace = [math]::Round($v.ProvisionedSpaceGB,2) # swap space included $vmName = $v.Name $vmNoDisks = ($v | Get-HardDisk).count $hash = New-Object PSObject -property @{Vm=$v.Name;NoDisks=$vmNoDisks;UsedSpaceNoSwap=$vmUsedSpaceNoSwap;UsedSpace=$vmUsedSpace;ProvSpace=$vmProvSpace} return $hash } |
We look at several parameters of the VM: memory reservation, allocated memory, used space, number of disks. We take the parameters and create a new PowerShell object that the function returns as a result.
The main body of the script is pretty simple. First, we define the format of the output CSV file and the we take every VM from vCenter Server (you need to be connected to vCenter Server before running this script) and process it:
1 2 3 4 5 6 7 8 9 | $vmData = @('"Name","NoDisks","UsedSpaceGB(noSwap)","UsedSpaceGB","ProvisionedSpaceGB"') $csvFile = ($MyInvocation.MyCommand.Path | Split-Path -Parent)+"\vmData.csv" foreach ($v in get-vm) { $hash = GetVMData -v $v $item = $hash.Vm + "," + $hash.NoDisks + "," + $hash.UsedSpaceNoSwap + "," + $hash.UsedSpace + "," + $hash.ProvSpace $vmData += $item } $vmData | foreach { Add-Content -Path $csvFile -Value $_ } |
If you want to look only at VMs that are powered on, then you need to add an IF clause that checks the VM power state in the FOR loop before processing each VM:
1 2 3 | if ($v.PowerState -match "PoweredOn") { # process $v } |
Not it's time to look at the output CSV file and start sizing for that backup solution.
1 comment:
Thanks for a grreat read
Post a Comment