Private/SearchAd.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
function SearchAd {
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('User','Group','OrganizationalUnit')]
        [string]$ObjectType
    )

    $searchString = switch ($ObjectType) {
        'Group' {
            "(&(objectClass=group)(objectCategory=group)(name=$Name))"
        }
        'User' {
            "(&(objectClass=person)(name=$Name))"
        }
        'OrganizationalUnit' {
            "(&(objectClass=organizationalUnit)(name=$Name))"
        }
        Default {}
    }
    $searcher = [adsisearcher]$searchString
    $searcher.FindAll()
}