Defrag autoremediation on drives "named" in alerts

This is probably something trivial to a syncro powershell expert, but im struggling to find a way to automate defrag alerts resolution. The text in an example alert is “DriveName = F:” so Im hoping to figure out a way to parse out this drive F and automate a defrag on that particular disk.

Certainly open to documentation or ideas, or just hearing how others do it.

Thank you in advance!

Parsing it out is easy if you can get the text of the alert. I do not recall if that is possible.

With SSD drives, defragging is not really something you want to do. I would check and make sure the drive is not an SSD. Defragging an SSD drive can reduced the life of the drive and unlike mechanical drives, there is really not latency for any head to move across to find the additional data.

You can always run hey check in your script too see the fragmentation percentage of a drive and then run your defrag that way.

Defrag should be automatic, go into Defrag GUI and make sure it’s set to run on schedule, sometimes you even have to turn the schedule off and back on again so it’s recreated. The only alerts I get are for laptops/external drives that are rarely turned on/hooked up. Almost all internal drives should be SSD now unless massive cheap storage is needed, this is a good income opportunity if not (ssd upgrade or likely just replace machine).

IIRC there’s no way to pass information from an alert to an automated remediation script, but I could be wrong. So your script will need to check for the drive that needs defragmenting. I whipped up something that should work. This can take a long time to run so set your script timeout high if you want the logs. Keep in mind that will prevent any other scripts from running til it’s done/timesout. I haven’t done a ton of testing on this so YMMV, let me know how it goes if you use it.

Import-Module $env:SyncroModule -DisableNameChecking

# Get all the drive letters for non-SSD drives
# If you want to exclude USB external drives change to:
# $nonssd = Get-PhysicalDisk | Where-Object {$_.BusType -ne 'USB' } | ForEach-Object {
$nonssd = Get-PhysicalDisk | ForEach-Object { 
    $physicalDisk = $_ 
    $physicalDisk | Get-Disk | Get-Partition |
        Where-Object DriveLetter |
            Select-Object DriveLetter, @{ n = 'MediaType'; e = { $physicalDisk.MediaType } } |
                Where-Object { $_.MediaType -ne 'SSD' }
} | Sort-Object DriveLetter

# Filter down to only fixed drives (this includes USB hard drives but not flashdrives)
$fixeddrives = (Get-CIMinstance Win32_LogicalDisk | Where-Object {$_.DriveType -eq '3'}).DeviceID -replace ":"
foreach ($drive in $nonssd) {
    if ($fixeddrives -contains $drive.DriveLetter) {
        $driveletters = $drive

# Analyze each drive and defrag if needed
$defragdrives = @()
foreach ($drive in $driveletters) {
    "Analyzing drive $($drive.DriveLetter)"
    $analyze = Optimize-Volume -DriveLetter $drive.DriveLetter -Analyze -Verbose 4>&1
    if ($analyze -like "*It is recommended that you defragment this volume.*") {
        $defragdrives += $drive
        "Drive $($drive.DriveLetter) needs defragmenting"

# If there are drives to defrag, do so and close the alert
if ($defragdrives -ne '') {
    foreach ($drive in $defragdrives) {
        "Defragmenting $($drive.DriveLetter)"
        Optimize-Volume -DriveLetter $drive.DriveLetter -Defrag -Verbose
    Close-Rmm-Alert -Category "Defrag Trigger"
else {
    "No drives need defragmenting"

I found nothing built into syncro that allows grabbing/working with text from an alert (this would be a nice feature though!)

This would be used for mechanical legacy drives and backup USB drives.

You can always run hey check in your script too see the fragmentation percentage of a drive and then run your defrag that way.

What did you mean by this?

Thank you for your suggestion

Thank you for this! Ill try it out on one of our legacy clients that has a bunch of mechanical drives and report back.