Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Initiate Evaluation of ConfigMgr DCM Baselines

Posted by Trevor Sullivan on 2011/06/22


Introduction

Recently, I was working with Desired Configuration Management (DCM) in System Center Configuration Manager (SCCM / ConfigMgr) 2007 SP2 R3. During the development of configuration items (CIs) and baselines, it’s common to have to trigger baseline evaluations to ensure that the validation rules you’re writing are correct.

The most straightforward method of trigger a baseline analysis is to:

  1. Log into a system using RDP / terminal services
  2. Open the ConfigMgr Control Panel applet (32-bit only)
  3. Visit the Configurations tab
  4. Manually select each baseline and click the Evaluate button

image

In this screenshot, we’ve only got two baselines assigned to this server, but what if we have 10? 20? 100? Do you really want to manually click through all those to get immediate baseline analysis results? What if you had to trigger all those on a subset of your entire infrastructure, say … a few hundred servers? Would you rather hire a bunch of interns just to click through these dialogs, or click a button and let ‘er rip?

Let’s take a look at how we can trigger immediate evaluation of these baselines on a single system. Once we do that, you can take the same concept, wrap some code around it, and execute it against “X” number of systems.

Invoking Baseline Evaluations via PowerShell

WMI

Since SCCM and PowerShell both integrate heavily with Windows Management Instrumentation (WMI), we can easily automate many SCCM functions on both clients and servers using PowerShell. First, we need to locate the information we want to work with somewhere in the WMI repository. Thankfully, there’s a great tool for that called SAPIEN WMI Explorer (it’s available from their Downloads page, I believe).

If you’re familiar with SCCM, you’ll probably quickly notice the root\ccm WMI namespace once you fire up WMI Explorer — this is where most of the SCCM client functions “live.” Digging just a bit further, you’ll probably notice a child namespace of root\ccm, called “dcm.” Bingo! That’s what we’re looking for! Thankfully, unlike other namespaces, this one is quite simple – it only contains a single WMI class, called SMS_DesiredConfiguration. This WMI class represents the configuration baseline objects that are assigned to this SCCM client.

image

The properties on an instance of SMS_DesiredConfiguration.

image

The TriggerEvaluation() method definition on the SMS_DesiredConfiguration WMI class.

PowerShell

Now that we’ve talked about how to get the information we’re after, and how to trigger the baseline evaluations, let’s explore the code that is necessary to perform these actions.

Using PowerShell, we’ll first get a list of all the DCM baselines that are assigned to a SCCM client:

# Define the remote computer we are working against
$RemotePc = "MyComputer"
# Get a list of baseline objects assigned to the remote computer
$Baselines = Get-WmiObject -ComputerName $RemotePc -Namespace root\ccm\dcm -Class SMS_DesiredConfiguration

Next, we’ll pass the iterate over the list of baseline assignments, and pass their respective Name and Version properties into the TriggerEvaluation() method on the SMS_DesiredConfiguration class. It is a static method, not an instance method, meaning that we must call the method from the class, rather than the individual instances. There are two parameters that must be passed into this method: 1) the name of the baseline (its unique ID), and 2) the version of the baseline object. Both of these properties will be dynamically retrieved from each instance of SMS_DesiredConfiguration that we retrieved in the first line of PowerShell code above.

# For each (%) baseline object, call SMS_DesiredConfiguration.TriggerEvaluation, passing in the Name and Version as params
$Baselines | % { ([wmiclass]"\\$RemotePc\root\ccm\dcm:SMS_DesiredConfiguration").TriggerEvaluation($_.Name, $_.Version) }

That’s all there is to it! Now that we’ve got this code working, we can simply wrap it into a handy-dandy PowerShell function, and call it!

function Invoke-SccmBaselineEvaluation
{
    param (
        [parameter(Mandatory = $true)]
        $ComputerName
    )
    # Get a list of baseline objects assigned to the remote computer
    $Baselines = Get-WmiObject -ComputerName $ComputerName -Namespace root\ccm\dcm -Class SMS_DesiredConfiguration

    # For each (%) baseline object, call SMS_DesiredConfiguration.TriggerEvaluation, passing in the Name and Version as params
    $Baselines | % { ([wmiclass]"\\$ComputerName\root\ccm\dcm:SMS_DesiredConfiguration").TriggerEvaluation($_.Name, $_.Version) }
}

Clear-Host
& Invoke-SccmBaselineEvaluation RemoteComputer

The last line of this short script demonstrates how to call the function using the “&” call operator in PowerShell. For more information, see this Technet article.

One Step Further

To take this concept one step further, we can retrieve a list of computers from a SCCM collection and perform the task we just defined against all of them. Let’s take a look at how to do this:

# 1. Replace your SCCM server name (MySccmServer) and SCCM Site Code (LAB) with your own!
# 2. [Optional] Replace the collection ID (SMS00001) with a collection of your choosing
$CompList = Get-WmiObject –ComputerName MySccmServer –Namespace root\sms\site_lab –Class SMS_CollectionMember_a –Filter "CollectionID = 'SMS00001'" -Property Name,ResourceType
# For each computer in the list, filter it for ResourceType 5 (computer resources), and then invoke the function above, passing in the computer name
$CompList | ? { $_.ResourceType -eq 5 } | % { Invoke-SccmBaselineEvaluation $_.Name }

This code grabs a list of computers from the SMS_CollectionMember_a class, filtering it for resources in the SMS00001 collection. The second line passes this collection of resources into the built-in PowerShell Where-Object (short-hand is “?”), which filters it for ResourceType of 5, which indicates a computer object. Finally, the function we wrote above (Invoke-SccmBaselineEvaluation) is called, passing in the name of each computer in the PowerShell pipeline.

Conclusion

This article has demonstrated how to use Windows PowerShell in conjunction with Microsoft System Center Configuration Manager. The PowerShell code above shows how to invoke an evaluation cycle of Desired Configuration Management “configuration baselines” that have been assigned to a SCCM client.

For more information about Desired Configuration Management, please see the Microsoft Technet documentation.

For more information about Windows PowerShell, please see the Microsoft Script Center and the Hey Scripting Guy! blog by Ed Wilson at Microsoft.

The code and syntax highlighting for this article was created using the Quest PowerGUI PowerShell editor.

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

 
%d bloggers like this: