Skip to content

Instantly share code, notes, and snippets.

@pandieme
Created July 28, 2022 11:16
Show Gist options
  • Save pandieme/9b308c9bb73c0755528264bd9f4bc0b4 to your computer and use it in GitHub Desktop.
Save pandieme/9b308c9bb73c0755528264bd9f4bc0b4 to your computer and use it in GitHub Desktop.
Get System WMI Filter in Active Directory. Used in Group Policy WMI Filtering.
function Get-SystemWmiFilter {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]
$Name,
[Parameter(Mandatory)]
[string]
$DomainName
)
$DomainDistinguishedName = "DC=" + $DomainName.Replace('.', ',DC=')
$Properties = @(
'msWMI-Author'
'msWMI-ID'
'msWMI-Name'
'msWMI-Parm1'
'msWMI-Parm2'
'Created'
'Modified'
)
$WmiFilter = Get-ADObject `
-Filter { ObjectClass -eq "msWMI-Som" -and msWMI-Name -eq $Name } `
-SearchBase "CN=SOM,CN=WMIPolicy,CN=System,$DomainDistinguishedName" `
-SearchScope OneLevel `
-Properties $Properties
$Response = [PSCustomObject]@{
Id = $WmiFilter.'msWMI-ID'.Trim('{}')
Name = $WmiFilter.'msWMI-Name'
Description = $WmiFilter.'msWMI-Parm1'
Query = $WmiFilter.'msWMI-Parm2'
Author = $WmiFilter.'msWMI-Author'
Created = $WmiFilter.Created
Modified = $WmiFilter.Modified
}
return $Response
}
@pandieme
Copy link
Author

PS> Get-SystemWmiFilter -Name 'Windows Server 2019' -DomainName 'ad.yourcompany.com'

Id          : 41F6FC01-5F07-4A64-97E5-9C63B3A78BB2
Name        : Windows Server 2019
Description : Only apply to Windows Server 2019 computers
Query       : 1;3;10;67;WQL;root\CIMv2;Select * from Win32_OperatingSystem where Version like "10.0.17763";
Author      : [email protected]
Created     : 7/26/2022 4:54:21 PM
Modified    : 7/28/2022 10:59:15 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment