-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLDAP-Query.ps1
65 lines (55 loc) · 1.94 KB
/
LDAP-Query.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
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2018 v5.5.150
Created on: 5/04/2018 8:41 AM
Created by: Marc Collins
Filename: LDAP-Query.ps1
===========================================================================
.DESCRIPTION
Get Users & Groups via LDAP Query.
Without having to install AD Powershell Module
.EXAMPLE Groups:
Get-LDAPGroup -Prefix "XXX" #Returns All Groups with a Name Starting with XXX
Get-LDAPGroup -Name "YYY" #Returns the Group with the Name 'YYY'
Get-LDAPGroup -Name "ZZZ*" #Returns All Groups with a Name Starting with 'ZZZ*'
.EXAMPLE Users:
Get-LDAPUser -SAMAccountName Marc #Returns the User Account Properties for the Account Marc
Get-LDAPUser -SAMAccountName '*' #Returns All Accounts in the Active Directory
Get-LDAPUser -SAMAccountName 'svc_' #Returns All Accounts in the Active Directory that start with SVC_
.LINK
https://github.com/Nillth/PWSH-LDAP
#>
function Get-LDAPGroup
{
[CmdletBinding(DefaultParameterSetName = 'GroupName')]
param
(
[Parameter(ParameterSetName = 'GroupPrefix')]
[string]$Prefix,
[Parameter(ParameterSetName = 'GroupName')]
[string]$Name
)
switch ($PSCmdlet.ParameterSetName)
{
GroupPrefix{ $Filter = "(&(objectCategory=group)(cn=$Prefix*))" }
GroupName{ $Filter = "(&(objectCategory=group)(cn=$Name))" }
}
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$FoundGroups = $Searcher.FindAll()
return $FoundGroups
}
function Get-LDAPUser
{
param
(
[string]$SAMAccountName
)
$Filter = "(&(objectCategory=User)(samAccountName=$($SAMAccountName)))"
$Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter = $Filter
$FoundUser = $Searcher.FindAll()
$UserEntry = $FoundUser.GetDirectoryEntry()
return $UserEntry
}