DRV Frag Autofix
Summary
The DRV Frag Autofix task is an automation task launched by the DRV - Frag Monitoring monitor set whenever a fragmentation breach requires automatic remediation. It reads the local JSON configuration file and the breach state file, performs defragmentation on eligible drives, rechecks fragmentation, and manages the ConnectWise ticket lifecycle via webhooks.
It runs only when the effective mode is AutoFix and the server autofix policy allows it. However, in this solution, servers are effectively restricted to AlertOnly mode: the dynamic group that receives the automation tasks (DRV Frag Monitoring - Active) excludes servers with endpoint‑level DRV_Frag_Mode set to Enabled - Autofix. Therefore, this task effectively performs remediation only on workstations configured with AutoFix. Servers will never be in a state that triggers defragmentation, and in AlertOnly or Disabled modes the script exits cleanly without any action.
How it works
-
Configuration and State Loading
The script first reads the configuration file:C:\ProgramData\_Automation\Script\DRVFragmentationMonitoring\DRVFragmentationMonitoring.jsonIt checks the effective
Mode,Role,Drives,Threshold, andAutoFixAllowed. If the mode isDisabledorAlertOnly, the script exits immediately. It also loads the current state fromDrives_With_Existing_Ticket.json, the close list fromDrives_To_Close_Ticket.json, and the fragmentation cache.Note: For servers, even if the configuration file shows
AutoFixAllowed = true(which would require the endpoint overrideEnabled - Autofix), such servers are not part of the Active group and are never targeted by the monitor. As a result, the Autofix task never runs on a server in this solution. The server autofix policy is only a safeguard in the scripts, not a practical path to server remediation. -
Lock and Remediation
A lock file (Autofix.lock) is created to prevent concurrent runs. For each drive in the state that is inAutoFixmode and not exhausted, the script:- Validates drive selection and media eligibility (only fixed HDDs).
- Measures pre‑remediation fragmentation via
Win32_Volume.DefragAnalysis. - If fragmentation is already below threshold, it skips defragmentation and allows the monitor to close the ticket.
- Otherwise, it performs defragmentation using
Win32_Volume.Defrag, then measures post‑remediation fragmentation. - Updates attempt count, timestamps, and state.
-
Ticket Lifecycle
- If the first remediation attempt fails, the script creates a ticket via the webhook.
- If a middle attempt fails and a ticket exists, it adds a comment.
- If the final attempt (attempt 4) fails, it adds a final comment and marks the state exhausted.
- If remediation succeeds and a ticket exists, it closes the ticket.
- If remediation succeeds and no ticket exists, the state is cleared.
- The fragmentation cache is updated with the post‑remediation measurement so the monitor observes the change immediately.
-
Cleanup
The lock file is removed in afinallyblock, even if an error occurs. The state files are written back, and the script returns a single summary string beginning withSuccess:orFailure:.
The Autofix task is not scheduled; it is triggered automatically by the monitor set when a failure string is returned. The task itself is configured as an Automation Task in CW RMM, so it runs only when the monitor requests it.
Dependencies
- Custom Field: DRV_Frag_Mode_Wks
- Custom Field: DRV_Frag_Mode_Svr
- Custom Field: DRV_Frag_Drives_Wks
- Custom Field: DRV_Frag_Drives_Svr
- Custom Field: DRV_Frag_Threshold_Wks
- Custom Field: DRV_Frag_Threshold_Svr
- Custom Field: DRV_Frag_Mode_Wks_Site
- Custom Field: DRV_Frag_Mode_Svr_Site
- Custom Field: DRV_Frag_Drives_Wks_Site
- Custom Field: DRV_Frag_Drives_Svr_Site
- Custom Field: DRV_Frag_Threshold_Wks_Site
- Custom Field: DRV_Frag_Threshold_Svr_Site
- Custom Field: DRV_Frag_Mode
- Custom Field: DRV_Frag_Drives
- Custom Field: DRV_Frag_Threshold
- Custom Field: Ticket_Mgmt_Webhook_Url
- Device Group: DRV Frag Monitoring - Active
- Device Group: DRV Frag Monitoring - Alert Only [Workstations]
- Task: DRV Frag Monitoring Configuration Writer
- Monitor: DRV - Frag Monitoring
- Workflow: CWRMM Ticket Management for Monitors
- Trigger: CWRMM Ticket Management for Monitors
- Solution: Drive Fragmentation Monitoring
Task Setup Path
- Tasks Path:
AUTOMATION➞Tasks - Task Type:
Automation Task
Task Creation
Description
- Name:
DRV Frag Autofix - Description:
Performs automatic defragmentation for drives breached in AutoFix mode and manages ConnectWise ticketing.
Max attempts = 4, retry interval = 24 hours.
- Category:
Monitoring

Script Editor
Row 1 Function: PowerShell script
- Notes:
<Leave it Blank> - Use Generative AI Assist for script creation:
False - Expected time of script execution in seconds:
3600 - Continue on Failure:
False - Run As:
System - Operating System:
Windows - PowerShell Script Editor:

Row 2 Function: Script Log
- Notes:
<Leave it Blank> - Continue on Failure:
False - Operating System:
Windows - Script Log Message:
%Output%
Completed Task

Output
- Script Log
- Updated state files:
Drives_With_Existing_Ticket.jsonDrives_To_Close_Ticket.jsonDrive_Fragmentation_Cache.json
Trigger
The task is triggered automatically by the DRV - Frag Monitoring monitor set when a failure string is returned. No schedule is defined in the task itself; it runs only when the monitor requests it.
Changelog
2026-08-26
- Initial version of the document