Friday, October 29, 2010

VBS for Enumerating E-mail Addresses in an OU

Recently I had to write a script that enumerated all e-mail addresses in a give OU and child OUs. This vbs allowed me to do so with relative ease, and then it output the results to a CSV.  I've modified it so it takes input for the output and then the initial OU. Remember since the root DSE is already found, please only input the child OUs, omitting the DSE base. Please take a look:



on error resume next
Const ADS_PROPERTY_APPEND = 3
Const ADS_PROPERTY_DELETE = 4
Dim qQuery, objConnection, objCommand, objRecordSet, obj
Dim oRootDSE, strDomain


Set oRootDSE = GetObject("LDAP://rootDSE")
strDomain = oRootDSE.get("defaultNamingContext")


Set fso = Wscript.CreateObject("Scripting.FilesystemObject")
FileName= "C:\file_name.csv"  


FileName = InputBox("Please input the destination and name of the file you want to export:","Input Filename","c:\file_name.csv")
strOU = InputBox("Please input the OU you want to PARSE:","Input OU","OU=Computers")


set fsHandle = fso.OpenTextFile (FileName,8,True)
' other categories = computer, user, printqueue, group
qQuery = ";" & _
        "(objectCategory=user)" & _
       ";distinguishedName;subtree"


Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection


wscript.Echo "Name, Address"
wscript.Echo "-=-=-=-=-=-=-"
fsHandle.Writeline "Name, Address"
objCommand.CommandText = qQuery
Set objRecordSet = objCommand.Execute


While Not objRecordSet.EOF
    Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))
    arrEmails = objUser.ProxyAddresses
    For Each email In arrEmails
        If Left(email,4)="SMTP" Then
Wscript.Echo objUser.CN + ", " + email
fsHandle.Writeline objUser.CN + ", " + email
End IF
    Next
    objRecordSet.MoveNext
Wend


objConnection.Close


fsHandle.Writeblanklines 1
fsHandle.close
set fso = Nothing




set objShell = wscript.createobject("wscript.shell")


MsgBox "Your report has been generated! Click OK to view in Notepad",48,"Success!"
objShell.Run "notepad " + FileName 

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...