How To: Enable Defrag on Server 2008R2 and Windows 7 Using ConfigMgr Setttings Management
August 14, 2012 1 Comment
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:
- Choose a name (Such as Enable Defrag).
- Choose the supported platforms (all Windows 7, all Server 2008 R2)
- Create a new Setting, choose Script for the setting type, and Boolean for the data type.
- 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}
- 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)
- Create a new Compliance Rule tab of the configuration item, where Enabled = True, as shown next:
- 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.
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! 😉