Skip to content

Instantly share code, notes, and snippets.

@ikonst
Created August 22, 2012 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ikonst/3420651 to your computer and use it in GitHub Desktop.
Save ikonst/3420651 to your computer and use it in GitHub Desktop.
Gets Active Directory user information into a VCF file of VCards you can import into your phone or contacts app
set iAdRootDSE = GetObject("LDAP://RootDSE")
namingContext = iAdRootDSE.Get("defaultNamingContext")
If WScript.Arguments.Count = 1 Then
namingContext = WScript.Arguments.Item(0)
Else
Wscript.Echo "Usage: ActiveDirectoryToVcf.vbs [Naming Context]"
Wscript.Echo "Default path is " & namingContext
End If
Function getOrgName(ou)
org = ""
parts = Split(ou.distinguishedName, ",")
for each part in parts
subparts = Split(part, "=")
if subparts(0) = "OU" then
if org <> "" then org = subparts(1) & "," & org
else org = subparts(1)
end if
next
getOrgName = org
End Function
Sub dumpUser(parent, user)
' Ignore uninteresting people
if user.telephoneNumber = "" and user.mobile = "" and user.mail = "" Then Exit Sub
' Ignore service accounts
if LCase(title) = "service account" or LCase(title) = "service acount" then Exit Sub
vcard = vcard & "BEGIN:VCARD" & vbcrlf
vcard = vcard & "VERSION:2.1" & vbcrlf
vcard = vcard & "N:" & user.sn & ";" & user.givenName & ";;;" & vbcrlf
vcard = vcard & "FN:" & user.displayname & vbcrlf
org = getOrgName(parent)
vcard = vcard & "ORG:" & org
if user.department <> "" then vcard = vcard & ";" & user.department
vcard = vcard & vbcrlf
if user.title <> "" then vcard = vcard & "TITLE:" & user.title & vbcrlf
if user.telephoneNumber <> "" then vcard = vcard & "TEL;WORK;VOICE:" & user.telephoneNumber & vbcrlf
if user.mobile <> "" then vcard = vcard & "TEL;MOBILE;VOICE:" & user.mobile & vbcrlf
if user.mail <> "" then vcard = vcard & "EMAIL;WORK;INTERNET:" & user.mail & vbcrlf
vcard = vcard & "REV:" & user.whenChanged & vbcrlf
vcard = vcard & "END:VCARD" & vbcrlf
End Sub
Sub recurse(ou)
For Each child In ou
if child.class = "user" then dumpUser ou, child
if child.class = "organizationalUnit" then recurse child
Next
End Sub
' Start recursion
Set root = GetObject("LDAP://" & namingContext)
vcard = ""
recurse root
' Write the VCF
Wscript.echo "Writing export.vcf"
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.CreateTextFile("export.vcf", 2, -1)
wfile.WriteLine vcard
wfile.Close
set wfile = nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment