Using a Group Policy WMI filter to test for a registry value

by Jeremy Saunders on May 10, 2009

I wanted a clean and simple way to prevent group policies applying during an automated build process. This is because it can potentially cause some of the components of the build process to fail. One of the last tasks to run on all servers built using my unattended build method will stamp the registry with the build version and date, so I was pretty eager to implement a WMI filter that would read the build version to determine whether or not the policy should apply. Easier said than done! I found a great article on WMI Filters that went into the requirements and gave an example, but I found that I still needed to do a considerable amount of research in order for everything to start making sense.

Copy the below MOF file to…

%SystemRoot%\System32\wbem

Compile it by typing…

Mofcomp -class:forceupdate BuildInfo.mof

Test it by typing…

Wmic path BuildInfo WHERE (BuildVersion Like “2.%”) get BuildVersion

This will return the build number from the registry for all 2.x builds

WMI Filter will be…

select * from BuildInfo where (BuildVersion Like “2.%”)

This will return True for all version 2.x builds

Please review the MOF file below. It is fully documented, and will help anyone who wants to implement the same process.

Enjoy!

BuildInfo.MOF


///////////////////////////////////////////////////////////////////////////
// BuildInfo MOF File
// This MOF File makes it possible to query for the server build version
// and date with a Group Policy WMI filter. It's specifically handy for
// Citrix XenApp builds, as it prevents the GPO from applying during a
// server build that potentially cause some of the components of the build
// process to fail.
//
// Release 1.0
// Written by Jeremy@jhouseconsulting.com.au on 6th May 2009.
//
// Copy it to...
//     %SystemRoot%\System32\wbem
//
// Compile it by typing...
//     mofcomp.exe -class:forceupdate %SystemRoot%\System32\wbem\BuildInfo.mof
//
// Test it by typing...
//     Wmic path BuildInfo WHERE (BuildVersion Like "2.%") get BuildVersion
// This will return the build number from the registry for all 2.x builds
//
// WMI Filter will be...
//     select * from BuildInfo where (BuildVersion Like "2.%")
// This will return True for all version 2.x builds
//
// Reference: http://grouppolicy.editme.com/WMIFilters
///////////////////////////////////////////////////////////////////////////

// First we must register the System Registry Provider by declaring and
// creating the Property Provider used to enumerate registry data and
// values from specific registry locations.
// It is important to understand that if the data that you want to report
// is contained within several subkeys in a registry location, you must
// use an Instance Provider instead.

#pragma namespace ("\\\\.\\root\\cimv2")

instance of __Win32Provider as $PropProv
{
Name="RegPropProv";
Clsid="{72967901-68EC-11d0-B729-00AA0062CBB7}";
};

instance of __PropertyProviderRegistration
{
Provider=$PropProv;
SupportsPut = TRUE;
SupportsGet = TRUE;
};

// Now we define the registry Class With Qualifiers
#pragma namespace ("\\\\.\\root\\cimv2")

[DYNPROPS]
class BuildInfo
{
     [key]string  Keyname="";
     string       BuildVersion;
     string       BuildDate;
};
[DYNPROPS]
instance of BuildInfo
{
     KeyName="BuildInfo";
     [PropertyContext("Local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Datacom\\Build|Version"), Dynamic, Provider("RegPropProv")] BuildVersion;
     [PropertyContext("Local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Datacom\\Build|Build Date"), Dynamic, Provider("RegPropProv")] BuildDate;
};
///////////////////////////////////////////////////////////////////////////
Jeremy Saunders

Jeremy Saunders

Independent Consultant | Contractor | Microsoft & Citrix Specialist | Desktop Virtualization Specialist at J House Consulting
Jeremy is a highly respected, IT Professional, with over 30 years’ experience in the industry. He is an independent IT consultant providing expertise to enterprise, corporate, higher education and government clients. His skill set, high ethical standards, integrity, morals and attention to detail, coupled with his friendly nature and exceptional design and problem solving skills, makes him one of the most highly respected and sought after Microsoft and Citrix technical resources in Australia. His alignment with industry and vendor best practices puts him amongst the leaders of his field.
Jeremy Saunders
Jeremy Saunders
Jeremy Saunders

Previous post:

Next post: