-
Notifications
You must be signed in to change notification settings - Fork 354
/
ldap_query_bin.nim
61 lines (47 loc) · 1.49 KB
/
ldap_query_bin.nim
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
#[
Author: @fkadibs
License: BSD 3-Clause
This is an example of querying Active Directory by using ADO's ADSI provider
]#
import winim/com
import strformat
# Connect to ADSI via ADO COM interface
var conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
#conn.Properties("User ID") = <username>
#conn.Properties("Password") = <password>
#conn.Properties("Encrypt Password") = true
conn.Open("Active Directory Provider")
# Create query object to connection
var command = CreateObject("ADODB.Command")
command.ActiveConnection = conn
# command.Properties("Page Size") = 100
# Retrieve DNS name of domain controller
var sysinfo = CreateObject("ADSystemInfo")
var dn = sysinfo.DomainDNSName
var root = fmt"<LDAP://{dn}>"
# Build and execute LDAP query
var queryFilter = "(&(objectCategory=person)(objectClass=user))"
var queryAttrib = "cn,distinguishedName"
var queryText = fmt"{root};{queryFilter};{queryAttrib};SubTree"
command.CommandText = queryText
var records = command.Execute()
# Check for empty recordset
if (records.BOF == true) and (records.EOF == true):
echo "No records found"
# Iterate over recordset
else:
records.MoveFirst()
while records.EOF == false:
# Iterate over row fields
var i = 0
var row: string
while i < records.Fields.Count:
var field = records.Fields.Item(i)
row = fmt"{row}{field} "
inc i
echo row
records.MoveNext()
# Cleanup
records.Close()
conn.Close()