$DebugPreference = "Continue"

# -------[Backup Config]------- #
# These must include a colon after the drive letter #
$osDiskLetter = 		"C:"
$bakDiskLetter = 		"Z:"
$backupsToKeep = 		"7"
$reqDiskSpacePercent = 	"15"
# ----------------------------- #

$osDiskSpaceRaw = Get-PSDrive $osDiskLetter.Trim(":") | Select-Object Used
$bakDiskSpaceRaw = Get-PSDrive $bakDiskLetter.Trim(":") | Select-Object Free
$osDiskSpaceGB = [math]::Round(($osDiskSpaceRaw.Used / 1024 / 1024 /1024),2)
$bakDiskSpaceGB = [math]::Round(($bakDiskSpaceRaw.Free / 1024 / 1024 /1024),2)
$reqDiskSpaceGB = [math]::Round(($osDiskSpaceRaw.Used / 100 * $reqDiskSpacePercent /1024 /1024 /1024),2)

Write-Host "---Created by Fil Sapia (v1.1)--"
Write-Host "--------[Configuration]---------"
Write-Host "OS Disk ($($osDiskLetter))     $($osDiskSpaceGB)GB Used"
Write-Host "Backup Disk ($($bakDiskLetter)) $($bakDiskSpaceGB)GB Free"
Write-Host "Required Space:  $($reqDiskSpaceGB)GB"
Write-Host "--------------------------------"

if ($bakDiskSpaceGB -lt $reqDiskSpaceGB)
{

    Write-Host "WARNING: Not enough free space to complete Windows Image Backup!"
    New-Item $PSScriptRoot\_NO_FREE_SPACE -Force | Out-Null
    Exit 1
}
else
{
    if (Test-Path $PSScriptRoot\_NO_FREE_SPACE -PathType Leaf)
    {Remove-Item $PSScriptRoot\_NO_FREE_SPACE}

    Write-Debug "Enough free space."
    Write-Host "Initiating backup process..."
    WBADMIN START BACKUP -backupTarget:$bakDiskLetter -include:$osDiskLetter -allCritical -quiet
    WBADMIN DELETE BACKUP -keepVersions:$backupsToKeep -quiet
    WBADMIN GET VERSIONS > $PSScriptRoot\WBAdmin_Output.txt
    Exit 0
}