Guest User

CheckMissingAdWmiFilters.ps1

a guest
Aug 25th, 2026
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # If you delete a WMI filter without unassigning it from all polices, the GPO editor won't unassign it
  2. # Any policies with it still assigned will deny assignment and the editor will claim the policy has no WMI filter
  3. # This script will report any policies claiming to have a WMI filter that can't be found in AD
  4. # Simplest way to solve it is by going to each policy and toggling WMI on (any filter) and off
  5. # (There may be a way to programmatically set the WMI filter)
  6.  
  7. [CmdletBinding()]
  8. param ()
  9.  
  10. function Get-gPCWQLFilterId {
  11.     [CmdletBinding()]
  12.     param (
  13.         [Parameter(Mandatory = $true)]
  14.         [String]
  15.         $DomainDn,
  16.         [Parameter(Mandatory = $true)]
  17.         [String]
  18.         $GpoId
  19.     )
  20.    
  21.     $dn = "CN={$GpoId},CN=Policies,CN=System,$DomainDn"
  22.  
  23.     $filter = (Get-ADObject -Identity $dn -Properties gPCWQLFilter -ErrorAction Stop).gPCWQLFilter
  24.     if ($null -ne $filter) {
  25.         return $filter.Split(";")[1]
  26.     }
  27. }
  28.  
  29. function Test-AdWmiFilter {
  30.     [CmdletBinding()]
  31.     param (
  32.         [Parameter(Mandatory = $true)]
  33.         [String]
  34.         $DomainDn,
  35.         [Parameter(Mandatory = $true)]
  36.         [String]
  37.         $GpoId
  38.     )
  39.  
  40.     $id = Get-gPCWQLFilterId -DomainDn $DomainDn -GpoId $GpoId -ErrorAction Stop
  41.     if ($null -eq $id) {
  42.         return $true
  43.     }
  44.     try {
  45.         $dn = "CN=$id,CN=SOM,CN=WMIPolicy,CN=System,$DomainDn"
  46.  
  47.         return $null -ne (Get-ADObject -Identity $dn)
  48.     }
  49.     catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
  50.         return $false
  51.     }
  52.     catch {
  53.         throw
  54.     }
  55. }
  56.  
  57. $DomainDn = $((Get-ADDomain).DistinguishedName)
  58. $gpos = Get-GPO -All
  59.  
  60. $gpos | Sort-Object DisplayName | ForEach-Object {
  61.     Write-Output "Checking '$($_.DisplayName)'..."
  62.     $result = Test-AdWmiFilter -DomainDn $DomainDn -GpoId $_.Id.ToString()
  63.     if (-not $result) {
  64.         Write-Warning -Message "Found an orphaned WMI filter"
  65.     }
  66.  
  67.     Write-Output ""
  68. }
Advertisement
Add Comment
Please, Sign In to add comment