Skip to content

Instantly share code, notes, and snippets.

@flyize
Created June 10, 2019 14:34
Show Gist options
  • Save flyize/ff78a44fa502c6b4cd10dd0e25212fb9 to your computer and use it in GitHub Desktop.
Save flyize/ff78a44fa502c6b4cd10dd0e25212fb9 to your computer and use it in GitHub Desktop.
PowerShell script used for Stablebit DrivePool. The goal is to offload files from an SSD pool to a magnetic pool in FIFO order.
Set-StrictMode -Version 1
# Script drivePoolMoves.ps1
<# .SYNOPSIS
Script will move files from one DrivePool to another according to FIFO policy
.DESCRIPTION
The script can be set to run as often as desired. The expected layout is a DrivePool consisting of two DrivePools, one magnetic and one solid state.
.NOTES
Author : fly (Zac)
#>
# Number of files to move before rechecking SSD space
$moveCount = 1
# Path to PoolPart folder on magnetic DrivePool drive
$archiveDrive = "E:\PoolPart.xxxx\Shares\"
# Path to PoolPart folder on SSD DrivePool drive
$ssdSearchPath = "F:\PoolPart.xxxx\Shares\"
# Minimum SSD drive use percent. Below this amount, stop archiving files.
$ssdMinUsedPercent = 65
# Maximum SSD drive use percent. Above this amount, start archiving files.
$ssdMaxUsedPercent = 85
# Do not move more than this many files
$fileArchiveLimit = 200
# Exclude these file/folder names
[System.Collections.ArrayList]$excludeList = @('*.covefs*', '*ANYTHING.YOU.WANT*')
# Other stuff
$ssdDriveLetter = ""
$global:ssdCurrentUsedPercent = 0
$fileNames = @()
$global:fileCount = 0
$errors = @()
Write-Output "---------------------------------------"
Write-Output "STARTING SCRIPT"
Write-Output "---------------------------------------"
function CheckSSDAbove($percent) {
$ssdDriveLetter = $ssdSearchPath.Substring(0, 2)
Get-WmiObject Win32_Volume |
Where-object {$ssdDriveLetter -contains $_.DriveLetter} |
ForEach {
$global:ssdUsedPercent = (($_.Capacity - $_.FreeSpace) * 100) / $_.Capacity
$global:ssdUsedPercent = [math]::Round($ssdUsedPercent, 2)
}
If ($ssdUsedPercent -ge $percent) {
Return $true
} Else {
Return $false
}
}
function MoveOldestFiles {
$fileNames = Get-ChildItem -Path $ssdSearchPath -Recurse -File -Exclude $excludeList |
Sort-Object CreationTime |
Select-Object -First $moveCount
If (!$fileNames) {
Write-Output "No files found to archive!"
Exit
}
ForEach ($fileName in $fileNames) {
Write-Output "Moving from: "
Write-Output $fileName.FullName
$destFilePath = $fileName.FullName.Replace($ssdSearchPath, $archiveDrive)
Write-Output "Moving to: "
Write-Output $destFilePath
New-Item -ItemType File -Path $destFilePath -Force
Move-Item -Path $fileName.FullName -Destination $destFilePath -Force -ErrorAction SilentlyContinue -ErrorVariable errors
If ($errors) {
ForEach($error in $errors)
{
if ($error.Exception -ne $null)
{
Write-Host -ForegroundColor Red "Exception: $($error.Exception)"
}
Write-Host -ForegroundColor Red "Error: An error occurred during move operation."
Remove-Item -Path $destFilePath -Force
$excludeList.Add("*$($fileName.Name)")
}
} Else {
Write-Output "Move complete."
$global:fileCount++ # Increment file count, then check if max is hit
If ($global:fileCount -ge $fileArchiveLimit) {
Write-Output "Archive max file moves limit reached."
Write-Output "Done."
Exit
} Else {
Write-Output "That was file number: $global:fileCount"
}
}
Write-Output "`n"
}
}
If (CheckSSDAbove($ssdMaxUsedPercent)) {
While (CheckSSDAbove($ssdMinUsedPercent)) {
Write-Output "---------------------------------------"
Write-Output "SSD is at $global:ssdUsedPercent%."
Write-Output "Max is $ssdMaxUsedPercent%."
Write-Output "Archiving files."
MoveOldestFiles
Write-Output "---------------------------------------"
}
} Else {
Write-Output "---------------------------------------"
Write-Output "SSD is at $global:ssdUsedPercent%."
Write-Output "Max is $ssdMaxUsedPercent%."
Write-Output "Drive not above max used."
Write-Output "---------------------------------------"
}
Write-Output "COMPLETE"
Write-Output "---------------------------------------"
Exit
@dsp9753
Copy link

dsp9753 commented May 28, 2021

Hey. Do you still use this script and has it been working well for you?

@flyize
Copy link
Author

flyize commented Jun 1, 2021

I switched to Unraid about a year ago, but it worked great before that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment