Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # If you delete a WMI filter without unassigning it from all polices, the GPO editor won't unassign it
- # Any policies with it still assigned will deny assignment and the editor will claim the policy has no WMI filter
- # This script will report any policies claiming to have a WMI filter that can't be found in AD
- # Simplest way to solve it is by going to each policy and toggling WMI on (any filter) and off
- # (There may be a way to programmatically set the WMI filter)
- [CmdletBinding()]
- param ()
- function Get-gPCWQLFilterId {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $true)]
- [String]
- $DomainDn,
- [Parameter(Mandatory = $true)]
- [String]
- $GpoId
- )
- $dn = "CN={$GpoId},CN=Policies,CN=System,$DomainDn"
- $filter = (Get-ADObject -Identity $dn -Properties gPCWQLFilter -ErrorAction Stop).gPCWQLFilter
- if ($null -ne $filter) {
- return $filter.Split(";")[1]
- }
- }
- function Test-AdWmiFilter {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $true)]
- [String]
- $DomainDn,
- [Parameter(Mandatory = $true)]
- [String]
- $GpoId
- )
- $id = Get-gPCWQLFilterId -DomainDn $DomainDn -GpoId $GpoId -ErrorAction Stop
- if ($null -eq $id) {
- return $true
- }
- try {
- $dn = "CN=$id,CN=SOM,CN=WMIPolicy,CN=System,$DomainDn"
- return $null -ne (Get-ADObject -Identity $dn)
- }
- catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
- return $false
- }
- catch {
- throw
- }
- }
- $DomainDn = $((Get-ADDomain).DistinguishedName)
- $gpos = Get-GPO -All
- $gpos | Sort-Object DisplayName | ForEach-Object {
- Write-Output "Checking '$($_.DisplayName)'..."
- $result = Test-AdWmiFilter -DomainDn $DomainDn -GpoId $_.Id.ToString()
- if (-not $result) {
- Write-Warning -Message "Found an orphaned WMI filter"
- }
- Write-Output ""
- }
Advertisement
Add Comment
Please, Sign In to add comment