Functions/Get-GPOWmiFilter.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Get-GPOWmiFilter
{
<#
.Synopsis
   Gets all Group Policy Filters or by name
.DESCRIPTION
   This cmdlet depends on the GroupPolicy module
.EXAMPLE
   Get-GPOWmiFilter
 
   Lists all (if any) WMIFilter configured in Group Policy Management
.EXAMPLE
   Get-GPOWmiFilter -Name HR
 
   Get a WMIFilter named HR
.EXAMPLE
   "HR" | Get-GPOWmiFilter
 
   Get a WMIFilter named HR using the pipeline
.EXAMPLE
   "HR","AllClients" | Get-GPOWmiFilter
 
   Gets WMIFilters named HR and Allclients using the pipeline
.INPUTS
   String (Name of WMIFilter)
.OUTPUTS
   Microsoft.GroupPolicy.WmiFilter
.NOTES
   Created by Tore.Groneng@firstpoint.no @ToreGroneng 2016
.ROLE
   Active Directory scripting
.FUNCTIONALITY
   Linking GPOs with WMIFilters
#>

[cmdletbinding()]
Param(
    [Parameter(ValueFromPipeline)]
    [string[]]$Name = "*"
)
BEGIN
{
    $f = $MyInvocation.InvocationName

    if(-not (Get-Module -Name GroupPolicy))
    {
        Import-Module -Name GroupPolicy -ErrorAction Stop -Verbose:$false
    }

    Write-Verbose -Message "$f - START"
    $GPdomain = New-Object Microsoft.GroupPolicy.GPDomain
    $SearchFilter = New-Object Microsoft.GroupPolicy.GPSearchCriteria
    Write-Verbose -Message "$f - Searching for WmiFilters"
    $allWmiFilters = $GPdomain.SearchWmiFilters($SearchFilter)
    Write-Verbose -Message "$f - Found $($allWmiFilters.Count) filters"
}

PROCESS
{
    foreach($FilterName in $Name)
    {
        Write-Verbose -Message "$f - Looking for $FilterName"
        $allWmiFilters | Where-Object Name -like $FilterName
    }
}

END
{
    Write-Verbose -Message "$f - END"
}
}