Here's the code to access AD (latest at here).

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
(* Accessing AD through LDAP
Inspired by http://stackoverflow.com/a/14814508/695964

Need nuget package System.DirectoryServices

*)

#r @"./packages/System.DirectoryServices/lib/System.DirectoryServices.dll"

open System
open System.Collections
open System.DirectoryServices

let de = new DirectoryEntry() // connects to the local domain controller

// these two are optional
de.Path <- "LDAP://OU=UserAccounts,DC=foo,DC=bar,DC=baidu,DC=com" // This scopes the subsequence queries
de.AuthenticationType <- AuthenticationTypes.Secure

let s = new DirectorySearcher(de, Filter="(name=John Smith)")

let res = s.FindOne()

res.Properties.["name"] // this is always a seq
res.Properties.["name"].[0] // this is always a obj that needs to be casted at runtime
res.Properties.["name"].[0] :?> string // I know it's a string

let myMailboxGuid = Guid(res.Properties.["someBinaryField"].[0] :?> byte array)

// Display all fields (res.Properties implements IDictionary: http://stackoverflow.com/a/3267704/695964)
res.Properties |> Seq.cast<DictionaryEntry> |> Seq.iter (fun x -> printfn "%A" (x.Key, x.Value))

References