tags:

views:

521

answers:

2

I'm trying to create a GPO programatically using the GPMC COM object via C# code. I can create the GPO, but I'm having trouble "inserting" a WMI filter with the GPO. Does anyone know how I can create/update WMI filters for GPOs?

+1  A: 

Here's the sample code:

GPMGMTLib.GPM gPM = new GPMGMTLib.GPM(); 
GPMConstants gPMConstants = gPM.GetConstants(); 
GPMDomain gPMDomain = gPM.GetDomain(domainName, DC, gPMConstants.UseAnyDC); 
GPMGPO obj = gPMDomain.CreateGPO(); 
obj.DisplayName = "New GPO";


//replace with the appropiate GUID
var strWMIFilterID = "{D715559A-7965-45A6-864D-AEBDD9934415}";
var sWMIFilter = string.Format("MSFT_SomFilter.Domain=\"{0}\",ID=\"{1}\"", domainName, strWMIFilterID);

var oWMIFilter = gPMDomain.GetWMIFilter(sWMIFilter); 
obj.SetWMIFilter(oWMIFilter);

Here are some links with additional information:
WMIFilters
Active Directory Cookbook

Jose Basilio
Yes, I've seen the WMI GPO Filtering Documentation, That's not what I'm trying to do...GPMGMTLib.GPM gPM = new GPMGMTLib.GPM();GPMConstants gPMConstants = gPM.GetConstants();GPMDomain gPMDomain = gPM.GetDomain(domainName, DC, gPMConstants.UseAnyDC);GPMGPO obj = gPMDomain.CreateGPO();obj.DisplayName = "New GPO";This is where I want to Add a WMI filter to the GPO. C# and GPO documentation is scarce... That is why I am asking here...
Julian Easterling
Also,var oWMIFilter = gPMDomain.GetWMIFilter("FilterName");obj.SetWMIFilter(oWMIFilter);would be the next step to add the WMI filter to the GPO. I missing the part that creates it...
Julian Easterling
I updated the post to incorporate your code and the syntax for getting the filter. Hope this helps.
Jose Basilio
A: 

Use this tools for translate code for you, it save code in C#, VB.Net or VB Script.
Play a bit with using this tool WMI Code Creator, good for learn WMI.

lsalamon