Search code examples
windowspowershellsccm

How can I use "IF" statement to determine the amount of free disk space to add to a VMDK?


The script I am using is below. We are upgrading our 1500 VDI's from Windows 10 1809 to 1909 and we want to automate the process as much as possible since this will be a process that we will have to do regularly with each new version Windows puts out. We are using SCCM (System Center Configuration Manager). The script works perfect for extending disk space for multiple machines. The .csv file it is importing only contains a list of virtual machines the script is to expand disk space on. I want to use an "if" statement to determine if the amount of free space is below 30GB and, if so, to add the needed disk space to bring it up to 30GB free space. Example: "if" free disk space is -lt 30GB, Set-HardDisk -CapacityGB (the difference to make 30GB free space available), "if" free space is -eq to 30GB, do nothing. I have researched Google and this site for anything remotely similar and wasn't able to find what I need. All help accomplishing this is appreciated.

##Task Change Disk Size
##Variable clear
$csvobjects = @()
$cskobject = @()
$network = @()
$isalive = @()

##Import VM name(s)
$csvobjects = Import-CSV -path "C:\Temp\ExpandHDDiskList.csv"

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

connect-viserver -server anyserver.com -User [email protected]

foreach ($csvobject in $csvobjects){
##Variable clear
$network = @()
$isalive = @()
##Pre-change data gathering
$beforechange = (GET-VM -Name $csvobject.vmname | FT -auto CapacityGB|out-string)
##Stop VM
GET-VM -Name $csvobject.vmname | Get-HardDisk -Name 'Hard disk 1' | Set-HardDisk -CapacityGB 75 -Confirm:$false
start-sleep -s 60

# Variable specifying the drive you want to extend
$drive_letter = "C"

# Script to get the partition sizes and then resize the volume
$size = (Get-PartitionSupportedSize -DriveLetter $drive_letter)
Resize-Partition -DriveLetter $drive_letter -Size $size.SizeMax
}

Solution

  • You can use Get-VMGuest to get the free space within the guest VM.

    Set the target amount of free space in a variable. 30GB (GB = 1024^3)

    $gb = 1024 * 1024 * 1024
    $space = 30 * $gb
    

    Get the free space from the VM using Get-VMGuest

    $vm = Get-VMGuest $csvobject.vmname
    

    Now, assuming a single hard disk in the machine, determine if the free space is less than 30GB. If it is, increase the hard disk size so that there is 30GB of free space. The calculation takes the capacity of the disk and adds on the required free space (30GB) and then subtracts the current free space. This is all done in bytes so convert it into GB and round it to a whole number to avoid weird disk sizes:

    if ($vm.Disks[0].FreeSpace -lt $space) {
       Get-HardDisk $vm.Vm | Select -first 1 | Set-HardDisk -CapacityGB ([math]::round(($vm.Disks[0].Capacity + $space - $vm.Disks[0].FreeSpace) / $gb))
    }
    

    Take care when running, particularly if you have machines with multiple hard disks.

    Assuming Windows VMs, you don't need to stop the VM. You can resize the filesystem remotely using Invoke-Command or Invoke-VMScript.