Results 1 to 7 of 7

Thread: Clearing a WMI filter from a GPO

  1. #1
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    What is the code to clear a wmi filter from GPO :?:

    I've tried using objGPOList.Item(1).SetWMIFilter(objWMIFilter) with an empty value for objWMIFilter to no available. Please advise. I have to script this to delete a wmi filter on hundreds of GPOs.

    Here's the MS reference to the setwmifilter method
    http://msdn2.microsoft.com/en-us/library/aa814230.aspx

  2. #2
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    Hi,

    Empty is not the same as NULL - which language are you using? (post the code if you wnat me to look at it).

  3. #3
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    As said in the reference you can use objGPOList.Item(1).SetWMIFilter(Null) to delete any WMI-filter on the gpo.

    GPMWMIFilter object to associate with the current GPO. Passing NULL in this parameter unlinks any existing WMI filters.

  4. #4
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    JakobHeidelberg: The code is in vbscript. This script parses out an html log file for GPO and WMI filter. It finds the GPO and supposed to remove the WMI filter.

    Fantomen: objGPOList.Item(1).SetWMIFilter(Null) results in a type mismatch error. I've tried passing null through another variable and it doesn't work.

    [code:1]Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")

    Const ForReading = 1
    On Error Resume Next

    Set objFSO = CreateObject("Scripting.FileSystemObject& quot;)
    Set objTextFile = objFSO.OpenTextFile("C:\wmilog.html&q uot;, ForReading)

    Do While objTextFile.AtEndOfStream <> True
    strLine = objtextFile.ReadLine
    If InStr(strLine, "]") Then
    arrGPORecord = split(strLine, "[")
    i = i + 1
    End If
    Loop
    key1 = InStr(arrGPORecord(1), "]")
    key3 = InStr(arrGPORecord(2), "]")
    strGPO = left(arrGPORecord(2), key3 - 1)
    strDomain = wshShell.ExpandEnvironmentStrings("%USERD NSDOMAIN%") ' e.g. rallencorp.com

    set objGPM = CreateObject("GPMgmt.GPM")
    set objGPMConstants = objGPM.GetConstants( )

    ' Initialize the Domain object
    set objGPMDomain = objGPM.GetDomain(strDomain, "", objGPMConstants.UseAnyDC)

    ' Find the GPO
    set objGPMSearchCriteria = objGPM.CreateSearchCriteria
    objGPMSearchCriteria.Add objGPMConstants.SearchPropertyGPODisplayName, _
    objGPMConstants.SearchOpEquals, _
    cstr(strGPO)
    set objGPOList = objGPMDomain.SearchGPOs(objGPMSearchCriteria&# 41;
    if objGPOList.Count = 0 then
    WScript.Echo "Did not find GPO: " & strGPO
    WScript.Echo "Exiting."
    WScript.Quit
    elseif objGPOList.Count > 1 then
    WScript.Echo "Found more than one matching GPO. Count: " & _
    objGPOList.Count
    WScript.Echo "Exiting."
    WScript.Quit
    else
    WScript.Echo "Found GPO: " & objGPOList.Item(1).DisplayName
    end if

    'on error resume next

    ' Link the filter and print the result
    objGPOList.Item(1).SetWMIFilter(Null&# 41;
    if Err.Number <> 0 then
    WScript.Echo "Failed to set WMI filter."
    WScript.Echo "Error: " & err.description
    else
    WScript.Echo "Set WMI filter successfully."
    end if[/code]

  5. #5
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    You're right, it does seem like a bug!

    You had me confused because you said you sent "an empty value", so I thought I had it head on with the Null thingy... But after testing I think theres a bug in the SetWMIFilter Method or hopefully just the documentation.

  6. #6
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    Leave it to me to solve my own problems. I ended up bypassing the GPM methods in favor of ADSI with this code:

    Set objROOTDSE = GetObject("LDAP://rootDSE")
    StrGPOpath = "LDAP://CN=" & strGPOID & ",CN=Policies,CN=System," & objRootDSE.Get("defaultNamingContext")
    Set objContainer = GetObject ("LDAP://CN=" & strGPOID & ",CN=Policies,CN=System," & objRootDSE.Get("defaultNamingContext"))
    objContainer.PutEx ADS_PROPERTY_CLEAR, "gPCWQLFilter", 0
    objContainer.SetInfo

    This ended up being faster and cleaner than using GPM.

  7. #7
    PreviousPoster is offline 100+ Helpful Posts! 50+ Helpful Posts
    Join Date
    Dec 1969
    Posts
    1,254

    Default

    Right, you must have felt abandoned! Nice job though :wink:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Friendly URLs by vBSEO