How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management

We found several systems recently where defrag was not enabled. Thanks to a little help from the TechNet forums, I was able to create a Compliance Settings Rule using PowerShell. Create a new compliance settings configuration item as follows:

      1. Choose a name (Such as Enable Defrag).
      2. Choose the supported platforms (all Windows 7, all Server 2008 R2)
      3. Create a new Setting, choose Script for the setting type, and Boolean for the data type.
      4. Paste the PowerShell code below for the Discovery Script:
        # Used by the RegisterTaskDefinition method
        $TASK_CREATE_OR_UPDATE = 0x6
        
        # Specify the computer name
        $computerName = "localhost"
        # Specify the task's name
        $taskName = "\Microsoft\Windows\Defrag\ScheduledDefrag"
        
        $taskService = new-object -comobject "Schedule.Service"
        $taskService.Connect($computerName)
        $taskFolder = $taskService.GetFolder("\")
        $registeredTask = $taskFolder.GetTask($taskName)
        $registeredTask.Enabled = $TRUE
        $taskDefinition = $registeredTask.Definition
        $taskDefinition.Triggers | % {$_.Enabled}
        
      5. Paste the PowerShell code below for the Remediation Script.
        $TASK_CREATE_OR_UPDATE = 0x6
        
        # Specify the computer name
        $computerName = "localhost"
        # Specify the task's name
        $taskName = "\Microsoft\Windows\Defrag\ScheduledDefrag"
        
        $taskService = new-object -comobject "Schedule.Service"
        $taskService.Connect($computerName)
        $taskFolder = $taskService.GetFolder("\")
        $registeredTask = $taskFolder.GetTask($taskName)
        $registeredTask.Enabled = $TRUE
        $taskDefinition = $registeredTask.Definition
        $taskDefinition.Triggers | foreach-object { $_.Enabled = $TRUE }
        [Void] $taskFolder.RegisterTaskDefinition($taskName, $taskDefinition,
         $TASK_CREATE_OR_UPDATE, $NULL, $NULL, $taskDefinition.Principal.LogonType)
        
      6. Create a new Compliance Rule tab of the configuration item, where Enabled = True, as shown next:
      7. Add this configuration item to a baseline and deploy to a collection. Note that in order to  execute Compliance Management scripts that use Powershell, you may need to modify the client setting named “PowerShell Execution Policy”  to Bypass.

Download EnableDefragOnServer08andWindows7.ps1.


About Greg Ramsey
Greg Ramsey is a systems engineer specializing in global systems management for Dell IT. He has a B.S. in Computer Sciences and Engineering from the Ohio State University and is a Microsoft Most Valuable Professional (MVP) for Microsoft System Center Configuration Manager. Greg coauthored SMS 2003 Recipes: A Problem-Solution Approach (Apress, 2006), Microsoft System Center Configuration Manager Unleashed (Sams, 2009), Microsoft System Center 2012 Configuration Manager Unleashed (Sams, 2012), and Microsoft System Center 2012 Configuration Manager: Administration Cookbook (Packt Publishing, 2012), and Microsoft System Center 2012 Configuration Manager R2 Unleashed (Sams, 2012). Greg is cofounder of the Ohio SMS Users Group, and the Central Texas Systems Management User Group.

One Response to How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management

  1. George Simos says:

    Great article Greg! Just a note for the readers, please use the “Copy to Clipboard” icon on the source code sections instead of the usual Ctrl+C combination because this way you will get the clear source code without the line numbers! 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: