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))