Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Export Active Directory Users to Excel Worksheet

 
Logged in as: Guest
arrSession:exec spGetSession 2,16,29830
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Post a VBScript >> Export Active Directory Users to Excel Worksheet
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2 3   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Export Active Directory Users to Excel Worksheet - 1/12/2006 9:32:46 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
This script when run will export every "user" object in Active Directory and put into an Excel Spreadsheet.  This will also include any and all SMTP alisas' (up to 20), that an account has.

Hope this might come in handy for someone.

UPDATED 3/7/2006  (please re-copy and paste if you use this already)  I had a small error in script which wasn't getting the email address property reported on the one that's displayed on the "General" tab in ADUC (it showed up in excel as blank).  Please note, there are 2 different locations for email, 1 on the General TAB in ADUC and another on the Email Addresses tab. (the one that was reporting blank was the one on the "General" tab.

-Eric

PS - I've posted the "write back" version of this script on another reply post.
















      

< Message edited by edavis6678 -- 3/7/2006 7:26:39 AM >
 
 
Revisions: 7 | Post #: 1
 
 RE: Export Active Directory Users to Excel Worksheet - 1/12/2006 3:33:51 PM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
have you tried this script on an AD domain that has more than 1000 users?

i have run into this problem before and know how to get around it, just wondering if yours is able to or not.

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to edavis6678)
 
 
Post #: 2
 
 RE: Export Active Directory Users to Excel Worksheet - 1/13/2006 11:05:33 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I haven't run it with over 1K users, but yes, it will not work unless you use ADO and LDAP query

(in reply to kirrilian)
 
 
Post #: 3
 
 RE: Export Active Directory Users to Excel Worksheet - 3/7/2006 12:38:08 AM   
  ronny

 

Posts: 2
Score: 0
Joined: 3/7/2006
Status: offline
Hi,

The Script is very usefull! thanks.

Could you post the code to write back to AD as well?

When it writes back to the Active Directory, does it write back every cell in the excel sheet, or only the updated fields?

Thanks!

Rgds,
Ronny

(in reply to edavis6678)
 
 
Post #: 4
 
 RE: Export Active Directory Users to Excel Worksheet - 3/7/2006 7:41:31 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
Ok...here's the script for writing back to AD. (you MUST use my original extract script for this to work). -careful with this one, please read script notes in beginning of file.

I've spend an hour or so cleaning it up and wrote in some logging. (didn't have it before, and now I think better).  Also,  in my original script I just re-commited all AD values, (even if just 1 property changed), but now this script will ONLY change values to those that changed, (and log it too).

I'm really glad to see that this script was helpful to you.  (and hopefully this write-back will be more helpful too).

Please let me know your feedback and how it works for you.

-Eric


      

(in reply to ronny)
 
 
Post #: 5
 
 RE: Export Active Directory Users to Excel Worksheet - 3/27/2006 10:21:37 PM   
  centrino

 

Posts: 16
Score: 0
Joined: 9/4/2005
Status: offline
IN Line 126 [Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName)] I have a "Subscript out of range" error. Any ideas?

(in reply to edavis6678)
 
 
Post #: 6
 
 RE: Export Active Directory Users to Excel Worksheet - 3/28/2006 3:47:38 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I'm not a pro of the excel object model, so my excel creation portion is probably prone to an error here or there..  I just ran the script from copying/pasting from forum to be sure it was copied up correctly and I didnt' receive error.  (seeing that I can't recreate, couple things come to mind.)

The script calls a sub to create the workbook, be sure line 7 is exactly as it is:  Call ExcelSetup("Sheet1")

This sub assumes that when you open Excel you have a default worksheet named, "Sheet1".  Verify this is the case for you by opening Excel and look at the sheet names.

The only thing thing that comes to mind is that the Excel object on your computer is not being created properly.  Can you try running this on another computer?

When you run the script and it gives an error, can you check your "task mananger" and see if EXCEL.EXE is running?





(in reply to centrino)
 
 
Post #: 7
 
 RE: Export Active Directory Users to Excel Worksheet - 5/9/2006 10:50:23 PM   
  Octal2001

 

Posts: 8
Score: 0
Joined: 4/12/2006
Status: offline
Nice Scripts, I have an question, i would like to export the ccmail field from AD do you know the object code i need to add to do this?

Thanks for any feed back.

(in reply to edavis6678)
 
 
Post #: 8
 
 RE: Export Active Directory Users to Excel Worksheet - 5/10/2006 3:30:16 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I'm not familar with CCmail...can there only be 1 CCMAIL address?  or multiples

I can give you a quick fix to this, but I'm thinking of adding some more fields per other users' requests.

Give me a day or so.

ps - if you know VB, this is how i got the properties


      

(in reply to Octal2001)
 
 
Revisions: 1 | Post #: 9
 
 RE: Export Active Directory Users to Excel Worksheet - 5/10/2006 9:22:08 PM   
  Octal2001

 

Posts: 8
Score: 0
Joined: 4/12/2006
Status: offline
Thanks for the code i'll test it now.there can only be one ccmail for each user account. (i Believe!!!)

(in reply to edavis6678)
 
 
Post #: 10
 
 RE: Export Active Directory Users to Excel Worksheet - 5/19/2006 6:33:47 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
I've made some small formatting changes to make it easier to read. I added an autosave at the end as well.


      

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to edavis6678)
 
 
Post #: 11
 
 RE: Export Active Directory Users to Excel Worksheet - 5/19/2006 6:49:06 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
oh..neat...i just tried it..very nice.

The clean-up is nice too.  I've been so slammed here at work, but I still have the code, (need to post it), which adds CCMAIL.  I'm also thinking of expanding the output for every AD property, and eventually also support over 1K users.  (the query just has to use a different provider).  Also want to add an option for computers too.  (which I already have written, again not cleaned up for publishing though).  (I even have another that does for local accounts - add and list, (a need that arose from creating a non-domain FTP server).  I'll post that sometime too  :)  (I know there are many free-ware scripts already written for this, but I think it's more fun to do it yourself)

Cheers

(in reply to kirrilian)
 
 
Post #: 12
 
 RE: Export Active Directory Users to Excel Worksheet - 5/22/2006 4:31:37 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
quote:

ORIGINAL: edavis6678

oh..neat...i just tried it..very nice.

Thanks, I got most of the formatting code from the script center. I've been wanting to play with the excel object and had a little time on Friday. :)
quote:


The clean-up is nice too.  I've been so slammed here at work, but I still have the code, (need to post it), which adds CCMAIL.  I'm also thinking of expanding the output for every AD property, and eventually also support over 1K users.  (the query just has to use a different provider).

Whenever you want to work on that I'd be interested in helping, although I can already see one problem. We have a custom AD schema here and a generic script that pulls all attributes might not work, so maybe pulling the schema first then enumerating all the attributes that way.

quote:


Also want to add an option for computers too.  (which I already have written, again not cleaned up for publishing though).  (I even have another that does for local accounts - add and list, (a need that arose from creating a non-domain FTP server).  I'll post that sometime too  :)  (I know there are many free-ware scripts already written for this, but I think it's more fun to do it yourself)

Again, I'd be happy to help implement doing computers rather than users as well.

Thanks for the excellent script!

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to edavis6678)
 
 
Post #: 13
 
 RE: Export Active Directory Users to Excel Worksheet - 5/26/2006 5:26:16 AM   
  tcole33

 

Posts: 6
Score: 0
Joined: 5/26/2006
Status: offline
Great job on the script. I was wondering if someone could help me modify this (your initial script).....
I would like it to return all of the users group memberships instead of the ADSPath.... for example

John    Doe
                        Domain User
                        Accounting
                        IT User
Jane Doe
                      Domain Users
                      Internet Access Group
                      Domain Administrator
etc......

I am trying to comply with SARBOX and this script is EXACTLY what I was looking for ... only I need the group memberships listed for each user......
Any help is greatly appreciated.

Here is the scirpt that you had posted and I had been modifying.......

Sub enumMembers(objDomain)
On Error Resume Next
Dim Secondary(20) ' Variable to store the Array of 2ndary email alias's
For Each objMember In objDomain ' go through the collection
If ObjMember.Class = "user" Then ' if not User object, move on.
x = x + 1 ' counter used to increment the cells in Excel

FirstName = objMember.GivenName
LastName = objMember.sn
Manager = ObjMember.Manager
AdsPath = Objmember.Adspath
zz = 1 ' Counter for array of 2ndary email addresses
For each email in ObjMember.proxyAddresses
  If Left (email,5) = "SMTP:" Then
Primary = Mid (email,6) ' if SMTP is all caps, then it's the Primary
  ElseIf Left (email,5) = "smtp:" Then
    Secondary(zz) = Mid (email,6) ' load the list of 2ndary SMTP emails into Array.
     zz = zz + 1
End If
Next
' Write the values to Excel, using the X counter to increment the rows.
objwb.Cells(x, 1).Value = FirstName
objwb.Cells(x, 2).Value = LastName
objwb.Cells(x, 3).Value = Manager
objwb.Cells(x, 4).Value = ADSPath
' Write out the Array for the 2ndary email addresses.
For ll = 1 To 20
objwb.Cells(x,26+ll).Value = Secondary(ll)
Next
' Blank out Variables in case the next object doesn't have a value for the property
FirstName = "-"
LastName = "-"
Manager = "-"
For ll = 1 To 20
Secondary(ll) = ""
Next
End If

' If the AD enumeration runs into an OU object, call the Sub again to itinerate

If objMember.Class = "organizationalUnit" or OBjMember.Class = "container" Then
     enumMembers (objMember)
End If
Next
End Sub
Sub ExcelSetup(shtName) ' This sub creates an Excel worksheet and adds Column heads to the 1st row
Set objExcel = CreateObject("Excel.Application")
Set objwb = objExcel.Workbooks.Add
Set objwb = objExcel.ActiveWorkbook.Worksheets(shtName)
Objwb.Name = "Active Directory Users" ' name the sheet
objwb.Activate
objExcel.Visible = True
objwb.Cells(1, 1).Value = "FirstName"
objwb.Cells(1, 2).Value = "LastName"
objwb.Cells(1, 3).Value = "Manager"
objwb.Cells(1, 4).Value = "Adspath"
End Sub
MsgBox "Done" ' show that script is complete

(in reply to kirrilian)
 
 
Post #: 14
 
 RE: Export Active Directory Users to Excel Worksheet - 5/26/2006 5:48:17 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
Removed and reposted in better location

< Message edited by edavis6678 -- 5/30/2006 4:18:06 AM >

(in reply to tcole33)
 
 
Revisions: 2 | Post #: 15
 
 RE: Export Active Directory Users to Excel Worksheet - 5/26/2006 11:18:48 AM   
  tcole33

 

Posts: 6
Score: 0
Joined: 5/26/2006
Status: offline
That was EXATLY what I was looking for! I can't thank you enough...... I wonder if I could get you to tell me 1 thing.... how would I add attributes from ad and include them into the database? I would like to pull the display name, group description, and manager attributes into the database as well if possible. That would give me everything I need.... Thanks again.... it has really helped me out.....

(in reply to edavis6678)
 
 
Post #: 16
 
 RE: Export Active Directory Users to Excel Worksheet - 5/27/2006 8:03:20 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
Very nice to hear that this working for.  It's nice to know that someone can find a script I find useful also useful.

As for other properties, it's pretty straight forward.  Below are some common properties.

SamAccountName = ObjMember.samAccountName
Cn = ObjMember.CN
FirstName = objMember.GivenName
LastName = objMember.sn
initials = objMember.initials
Descrip = objMember.description
Office = objMember.physicalDeliveryOfficeName
Telephone = objMember.telephonenumber
EmailAddr = objMember.mail
WebPage = objMember.wwwHomePage
Addr1 = objMember.streetAddress
City = objMember.l
State = objMember.st
ZipCode = objMember.postalCode
Title = ObjMember.Title
Department = objMember.Department
Company = objMember.Company
Manager = ObjMember.Manager
Profile = objMember.profilePath
LoginScript = objMember.scriptpath
HomeDirectory = ObjMember.HomeDirectory
HomeDrive = ObjMember.homeDrive
AdsPath = Objmember.Adspath
LastLogin = objMember.LastLogin


Here is a link from MS on the rest of them:  http://windowssdk.msdn.microsoft.com/library/default.asp?url=/library/en-us/ADSI/adsi/iadsuser.asp

Remember that some values are very long and some have values that don't insert well into Access unless you tweak them.  (That's why i have the FixValue function in my script).  Also, some values are COLLECTIONS, (so you have to write a routine to get it out).

I'm not on a domain atm, so I can't test these changes, if what I posted here doesn't work, let me know and I'll correct, but below is what you need to change.

If you want to change the properties, or add more, just simply add more colummns to your database table, set variables to the user object properties, then modify the INSERT command with the additional properties.

For example....

If you want to ADD another columm for Manager ADS path, modify the script this way:  (RED notes new lines)

First, you must add a colummn to your table and name it, ManagerADSPath.  Make it a MEMO type since it will return a very long value.

Next, add a variable named, ManagerADS = to the AD user object property.  Shown below.  (also note I purposely named it differently so you can see how the INSERT command works)


ADGroupName = Mid(FixValue(objADGroup.Name),4)
SamAccountName = FixValue(objMember.SamAccountName)
Department = FixValue(ObjMember.Department)
UserADSPath = FixValue(objMember.AdsPath)
   ManagerADS = FixValue(objMember.Manager)
qryInsert = "INSERT INTO [" & dbTableName & "](ADGroupName, SamAccountName, Department,CN, UserAdsPath, ManagerADSPath) VALUES ('" & ADGroupName & "','" & SamAccountName & "','" & Department & "', '" & CN & "', '" & UserAdsPath & "', '" & ManagerADS & '")"
conn.Execute (qryInsert)

Just add more and more if you want, or change some of them with what you want.

Special note.  Manager for a user object only returns an ADSPath.

Another thing you can do.  Take the excel output from the original script and IMPORT it into Access, then join the tables VIA a key field.  (like SamAccountName), and make your own report, query, etc....(you could also get the Display name of the manager, but simply buidling a query and linking ADSPATH)

Hope this makes sense

< Message edited by edavis6678 -- 5/27/2006 8:16:31 AM >

(in reply to tcole33)