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.


Show Software Update Compliance State for each System in a Collection

We had an internal request to “Show me software update compliance state for each system in a collection, based on an Update List”. There are a couple canned web reports available that got me close, but not exactly what I need.  So here it is in case anyone else is interested.

Here’s a shot of the report:


Here’s the SQL:

declare @CI_ID int; select @CI_ID=CI_ID from v_ConfigurationItems where CIType_ID=9 and CI_UniqueID=@AuthListID 

SELECT rs.Name0 AS MachineName, rs.User_Name0 AS LastLoggedOnUser, asite.SMS_Assigned_Sites0 AS AssignedSite, rs.Client_Version0 AS ClientVersion, 
(CASE cs.Status WHEN 0 THEN 'Compiance State Unknown' WHEN 1 THEN 'Compliant' WHEN 2 THEN 'Non-Compliant' WHEN 3 THEN 'Conflict Detected' ELSE 'Null' END) 
AS State, cs.StatusTime 
FROM v_ClientCollectionMembers AS ccm INNER JOIN 
v_UpdateListStatus_Live AS cs ON cs.CI_ID = @CI_ID AND cs.ResourceID = ccm.ResourceID INNER JOIN 
v_R_System AS rs ON rs.ResourceID = ccm.ResourceID LEFT OUTER JOIN 
v_RA_System_SMSAssignedSites AS asite ON asite.ResourceID = ccm.ResourceID 
WHERE (ccm.CollectionID = @CollID) 
order by rs.Name0

 

Download the report

Greg

ramseyg@hotmail.com