Create a WQL query setting for a Configuration Item in ConfigMgr 2012 via PowerShell

Before I start with this blog post I have to give some (read: a lot) of credits to Dexter. He helped me a lot with understanding the SDMPackageXML of a Configuration Item (CI) and also blogged about that experience. Also, this blog post won’t go into the details he already mentioned about modifying the XML and writing it to a CI.

Now let’s really start with this blog post. This blog post will be about creating a WQL query setting for a CI and more specifically the road to creating a WQL query setting for a CI.

Step 1: Locate the method to create the WqlQuerySetting

WqlQuerySettingThe first step is to locate the method that can be used to create the WqlQuerySetting. During the installation of the Configuration Manager Console many files are installed and registered including a DLL named Microsoft.ConfigurationManagement.DesiredConfiguration.dll. This specific DLL contains the functions that are needed to create settings and compliance rules for a CI. Opening this DLL with something like the Object Browser of Visual Studio will show the different methods and parameters included. This will show the following information about creating the setting WqlQuerySetting(Microsoft.ConfigurationManagement.DesiredConfiguration.Rules.DataType settingDataType, string logicalName, string name, string description).

Step 2: Locate and set the object information for the WqlQuerySetting

WqlSettingXMLThe second step is to set the information to different input parameters that are required to create the setting. Based on the information, found in the DLL, it’s clear what information is needed and based on an existing SDMPackageXML it’s also clear what it should look like. Specifically for setting the logicalName it’s good to know the prefix of the GUID (as it differs per setting type). Another thing to look at is the settingDataType. The easiest method to find correct values for this is by browsing to the object in Visual Studio. Together this brings me to the following input variables for creating the WqlQuerySetting:

#Set the WqlQuerySetting object information $WqlQuerySettingDataType = ([Microsoft.ConfigurationManagement.` DesiredConfiguration.Rules.ScalarDataType]::Int64) $WqlQuerySettingLogicalName = "WqlSetting_$([Guid]::NewGuid())" $WqlQuerySettingName = "$WqlQuerySettingClass"+"_"+"$WqlQuerySettingProperty" $WqlQuerySettingDescription = "Scripted setting"

Step 3: Create the WqlQuerySetting and set the properties

The third step is to really create the WqlQuerySetting object and to set the properties for the new object. Again the easiest method to find the available properties is by browsing the class. Together this brings me to the following code for creating the setting and setting the properties:

#Create the WqlQuerySetting object and set the properties $WqlQuerySetting = New-Object -TypeName Microsoft.ConfigurationManagement.` DesiredConfiguration.Settings.WqlQuerySetting -ArgumentList ` $WqlQuerySettingDataType,$WqlQuerySettingLogicalName,` $WqlQuerySettingName,$WqlQuerySettingDescription $WqlQuerySetting.Class = $WqlQuerySettingClass $WqlQuerySetting.Namespace = $WqlQuerySettingNamspace $WqlQuerySetting.Where = $WqlQuerySettingWhere $WqlQuerySetting.Property = $WqlQuerySettingProperty

One might notice that I used all variables to fill these properties. That’s because I use this as part of a complete method that requires the values to those properties as input, so it can be reused. An example for these properties would be something like the following:

#Set the WqlQuerySetting properties $WqlQueryClass = "Win32_OptionalFeature" $WqlQueryNamespace = "root\cimv2" $WqlQueryWhere = "Name='BITS'" $WqlQueryProperty = "InstallState"

Step 4: Write the WqlQuerySetting to the Configuration Item

CI_WqlSettingXMLThe fourth, and last, step is to add the WqlQuerySetting to a CI. To do this it is important to add it to the correct element of the SDMPackageXML. This is also were it differs from what Dexter did and by that also the reason why I’m mentioning it. The setting needs to be added to the RootComplexSetting element and the tricky part is that it’s an empty element at the beginning. The following code gets the right part of the WqlQuerySetting and adds it to the SDMPackageXML.

#Import the WqlQuerySettingXML node to the SDMPackageXML of the CI $ImportNodeXML = ` $CISDMPackageXML.ImportNode($WqlQuerySettingXML.SimpleSetting,$true) #Add the imported node to the SDMPackageXML of the CI $CISDMPackageXML.DesiredConfigurationDigest.OperatingSystem.Settings.` ChildNodes[0].AppendChild($ImportNodeXML)

>> The complete function is available via download here on the TechNet Galleries! <<

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.