Skip to main content

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​

  1. Configuration and State Loading
    The script first reads the configuration file:

    C:\ProgramData\_Automation\Script\DRVFragmentationMonitoring\DRVFragmentationMonitoring.json

    It checks the effective Mode, Role, Drives, Threshold, and AutoFixAllowed. If the mode is Disabled or AlertOnly, the script exits immediately. It also loads the current state from Drives_With_Existing_Ticket.json, the close list from Drives_To_Close_Ticket.json, and the fragmentation cache.

    Note: For servers, even if the configuration file shows AutoFixAllowed = true (which would require the endpoint override Enabled - 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.

  2. Lock and Remediation
    A lock file (Autofix.lock) is created to prevent concurrent runs. For each drive in the state that is in AutoFix mode 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.
  3. 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.
  4. Cleanup
    The lock file is removed in a finally block, even if an error occurs. The state files are written back, and the script returns a single summary string beginning with Success: or Failure:.

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​

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

Image1

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:

PowerShell Script

Image2

Row 2 Function: Script Log​

  • Notes: <Leave it Blank>
  • Continue on Failure: False
  • Operating System: Windows
  • Script Log Message: %Output%

Image3

Completed Task​

Image4

Output​

  • Script Log
  • Updated state files:
    • Drives_With_Existing_Ticket.json
    • Drives_To_Close_Ticket.json
    • Drive_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