Changing the ConfigMgr client health evaluator task (CCMEval)

My ConfigMgr 2012 book for training 10747A: Administering System Center 2012 Configuration Manager says on page 3-53 that:

This task runs ccmeval.exe at a time between 12:00AM and 1:00AM.

Ok, it’s fine to set a random time but not between midnight and 1:00AM…to avoid all computers sending info the Management Point at the same time.
Ok, the task is configured to run ASAP whenever a schedule is missed… for computers shut down at midnight and booting up in the morning.

If you’d like to introduce more randomness for this task, you can achieve it like this:

#Requires -Version 2

Function Set-CMCCMevalTaskTrigger {
<#
.SYNOPSIS
    Change the execution time of the CCMEval task

.DESCRIPTION
    Change the execution time of the CCMEval task by either setting a random start hour or a specified one

.PARAMETER ComputerName
    Array of string that represents the remote computers to target

.PARAMETER Hour
    Integer that sets the hour when the task will execute

.PARAMETER Random
    Switch to turn of a randomly chosen hour

.EXAMPLE
    Set-CMCCMevalTaskTrigger -Verbose
    Change to execution time on the local computer to 12:00AM instead of 12:00PM

.EXAMPLE
    "RemotePC1","RemotePC2" | Set-CMCCMevalTaskTrigger -Hour 13 -Verbose
    Change to execution time on the specified computers to 01:00PM

.EXAMPLE
    "RemotePC1","RemotePC2" | Set-CMCCMevalTaskTrigger -Random -Verbose
    Change to execution time on the specified computers to a random start time (hour)

#>
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=$true,ValueFromPipeLineByPropertyName=$true)]
[Alias('CN','__Server','IPAddress','Server','hostname')]
[string[]]$ComputerName=$env:COMPUTERNAME,

[Parameter()]
[Alias('StartTime','Time')]
[ValidateRange(0,23)]
[int]$Hour = 12,

[Parameter()]
[switch]$Random

)
Begin{
    # Make sure we run as admin...
    $usercontext = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
    # ...in international mode
    $IsAdmin = $usercontext.IsInRole(544)
    if (-not($IsAdmin)) {
        Write-Warning "Must run powerShell as Administrator to perform these WMI queries"
        break
    }
   
    $TASK_UPDATE_FLAG = 0x4

    if ($PSBoundParameters.ContainsKey('Random')) {
        $Hour = (Get-Random -Maximum 23 -Minimum 0)
        $NewTime = 'T{0:00}:' -f $Hour
    } else {
        $NewTime = 'T{0:00}:' -f $Hour
    }
}
Process {
    $ComputerName | ForEach-Object -Begin {
        $TaskService = New-Object -com schedule.service
    } -Process {
        $Computer = $_
        try {
            # Connect to target computer
            $TaskService.Connect($Computer)
            # Get the XML definition of the task
            $taskDef = $TaskService.GetFolder('\Microsoft\Configuration Manager').GetTask('Configuration Manager Health Evaluation').Definition
            # Loop through Calendar triggers to change the start time
            $taskDef.Triggers | ForEach-Object -Process {
                if ($_.StartBoundary) {
                    $_.StartBoundary = ($_.StartBoundary -replace "T\d{2}:",$NewTime)
                }
            }
            # Update the existing task
            $TaskService.GetFolder('\Microsoft\Configuration Manager').RegisterTaskDefinition(
              'Configuration Manager Health Evaluation',
              $taskDef,
              $TASK_UPDATE_FLAG,
              $null,
              $null,
              $taskDef.Principal.LogonType
            )
            Write-Verbose -Message "Successfully set CCMeval taks execution time to $Hour on $Computer"
        } catch {
            Write-Warning -Message "Failed to change CCMeval task for computer $Computer because $($_.Exception.Message)"
        }
    }
}
End {}
}

Advertisements

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