Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


RE: 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 >> RE: 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: <<   < prev  1 [2] 3   next >   >>
Login
Message << Older Topic   Newer Topic >>
 RE: Export Active Directory Users to Excel Worksheet - 5/30/2006 12:45:15 AM   
  tcole33

 

Posts: 6
Score: 0
Joined: 5/26/2006
Status: offline
sorry... i was referring to the post before yours.... I will try yours as well.......Thanks

(in reply to gdewrance)
 
 
Post #: 21
 
 RE: Export Active Directory Users to Excel Worksheet - 5/30/2006 12:56:04 AM   
  gdewrance


Posts: 586
Score: 3
Joined: 3/16/2006
Status: offline
here is another one

'==========================================================================
'
' NAME: GetGroupMembers.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: http://www.thespidersparlor.com
' DATE  : 3/23/2005
'
' COMMENT: This script and many more are available in
'          the Admin Script Pack by The Spider's Parlor
'==========================================================================


Dim sResultText,Grps,MemberList
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = oConnection

ldstring = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;"

objCommand.CommandText=ldstring & "(objectClass=group);name,SamAccountName"

Set oRecordSet = objCommand.Execute()
Do While Not oRecordSet.EOF
   sResultText = sResultText & oRecordSet.Fields("samAccountName") & vbCrLf
   'WScript.Echo oRecordSet.Fields("samAccountName") & vbCrLf
   MemberList=RetrieveUsers(dom,oRecordSet.Fields("samAccountName"))
   'WScript.Echo Memberlist
   sResultText = sResultText & memberlist & vbCrLf & "************************************" & vbCrLf
  
   oRecordSet.MoveNext
Loop
'Wscript.Echo sResultText


Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile (dom & "DomainGroupUsers.txt", ForWriting)
ts.write sResultText
MsgBox "Done"


'*****************************************************************************************
'*****************************************************************************************
Function RetrieveUsers(domainName,grpName)

dim dom
dim grp
dim GrpObj
dim mbrlist
dim mbr

'-------------------------------------------------------------------------------
' *** Enumerate Group Members ***
'-------------------------------------------------------------------------------

grp = grpName
Set objDomain = getObject("LDAP://rootDse")
domainName = objDomain.Get("dnsHostName")
' Build the ADSI query and retrieve the group object
Set GrpObj = GetObject("WinNT://" & domainName & "/" & grp & ",group")

' Loop through the group membership and build a string containing the names
for each mbr in GrpObj.Members
  On error resume next
  mbremail = SearchEmail(mbr.name) 
  If Err Then
      mbrlist = mbrlist & vbTab & mbr.name & vbCrLf
  Else
  'if you don't want the email addresses, then copy the line 2 up to below
      mbrlist = mbrlist & vbTab & mbr.name & vbTab & vbTab & mbremail+ vbCrLf
  End If
Next

'The next line returns mbrlist back up to the main body
RetrieveUsers=mbrlist

End Function

Public Function SearchEmail(ByVal vSAN)
   ' Function:     SearchDistinguishedName
   ' Description:  Searches the DistinguishedName for a given SamAccountName
   ' Parameters:   ByVal vSAN - The SamAccountName to search
   ' Returns:      The DistinguishedName Name
   Dim oRootDSE, oConnection, oCommand, oRecordSet

   Set oRootDSE = GetObject("LDAP://rootDSE")
   Set oConnection = CreateObject("ADODB.Connection")
   oConnection.Open "Provider=ADsDSOObject;"
   Set oCommand = CreateObject("ADODB.Command")
   oCommand.ActiveConnection = oConnection
   oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
       ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));mail;subtree"
   Set oRecordSet = oCommand.Execute
   On Error Resume Next
   SearchEmail = oRecordSet.Fields("mail")
   On Error GoTo 0
   oConnection.Close
   Set oRecordSet = Nothing
   Set oCommand = Nothing
   Set oConnection = Nothing
   Set oRootDSE = Nothing
End Function

(in reply to tcole33)
 
 
Post #: 22
 
 RE: Export Active Directory Users to Excel Worksheet - 5/30/2006 4:47:55 AM   
  tcole33

 

Posts: 6
Score: 0
Joined: 5/26/2006
Status: offline
I finally have it working... sorry... it took me a little bit to understand how it fit together......I have 1 question.... is there any way to pull in the manager name without the path that you know of? Everything else is working like a charm........ the script is SUPER.... Thanks so much!!!!!!!!!!!!

(in reply to edavis6678)
 
 
Post #: 23
 
 RE: Export Active Directory Users to Excel Worksheet - 5/30/2006 4:54:10 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I have removed my group membership script from this thread and moved it to here:

http://www.visualbasicscript.com/m_34962/mpage_1/key_/tm.htm#34962

It's been heavily modified and includes Group Description and auto-table creation.

(in reply to tcole33)
 
 
Revisions: 1 | Post #: 24
 
 RE: Export Active Directory Users to Excel Worksheet - 6/2/2006 5:39:06 AM   
  tcole33

 

Posts: 6
Score: 0
Joined: 5/26/2006
Status: offline
Just wanted you to know that this script rocks. Can't thank you enough for it.... I was wondering if I could possibly pick your brain and ask if there is a way to write this information back to active directory from the access database? I guess what I'm wondering is that I am also in the middle of building a test lab. I have imported all of the users, ous, and groups into my test lab but using ldifde I am not able to import the group memberships with the groups. I was wondering if this could actually push everything to my test lab from the database......make sense? The code I am using is below to build the access database (your script modified to my env.). Thanks.

Dim conn, dbpath, sqlcommand, dbTableName
Dim UserName, Department, CN
Dim ObjMember, ObjGroup, strGroupName, strActiveGroup, ObjGroupMember
Dim strDN, objMemberList
Dim objConnection, objCommand, objRootDSE, strDNSDomain
' Dictionary object to track group membership.
Set objMemberList = CreateObject("Scripting.Dictionary")
objMemberList.CompareMode = vbTextCompare
'######################################
dbPath = "C:\Scripts\MUG\Access\tblgroupsDb.mdb"
'######################################
dbTableName = "tblGroups"
Set conn = CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open dbpath
SqlCommand = "DELETE * FROM [" & dbTableName & "]"
conn.Execute (SqlCommand)
Set objRoot = GetObject("LDAP://RootDSE")
strDNC = objRoot.Get("DefaultNamingContext")
strDNSDomain = objRoot.Get("defaultNamingContext")
Set objOU = GetObject("LDAP://" & strDNC)
' Setup ADO.
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 10000
objCommand.Properties("Timeout") = 99
objCommand.Properties("Cache Results") = False
Call enummembers(objOU)
Sub enumMembers(ActiveObjectOU)
  For Each objGroupMember In ActiveObjectOU
If objGroupMember.Class = "group" Then
Call EnumGroup(ObjGroupMember)
End If
If objGroupMember.Class = "organizationalUnit" or objGroupMember.Class = "container" Then
          enumMembers (ObjGroupMember)
      End If
Next
End Sub
Sub EnumGroup(objADGroup)
Dim strFilter, strAttributes, objRecordSet, intGroupToken
Dim objMember, strQuery, strNTName
' Retrieve "primaryGroupToken" of group.
objADGroup.GetInfoEx Array("primaryGroupToken"), 0
intGroupToken = objADGroup.Get("primaryGroupToken")
' Use ADO to search for users whose "primaryGroupID" matches the
' group "primaryGroupToken".
strFilter = "(primaryGroupID=" & intGroupToken & ")"
strAttributes = "sAMAccountName"
strQuery = "<LDAP://" & strDNSDomain & ">;" & strFilter & ";" _
  & strAttributes & ";subtree"
  'WScript.echo strQuery
objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
  strNTName = objRecordSet.Fields("sAMAccountName")
       'ADGroupName = FixValue(ObjADGroup.AdsPath)' use this for ADGroupName if you just want the ADS Path
ADGroupName = Mid(FixValue(ObjADGroup.Name),4)
       SamAccountName = FixValue(strNTName)
       Department = "Test"
       UserAdsPath = "Ads"
       qryInsert = "INSERT INTO [" & dbTableName & "](ADGroupName, SamAccountName, Department,CN, UserAdsPath) VALUES ('" & ADGroupName & "','" & SamAccountName & "','" & Department & "', '" & CN & "', '" & UserAdsPath & "')"
conn.Execute (qryInsert)
  objRecordSet.MoveNext
Loop
For Each objMember In objADGroup.Members
  'ADGroupName = FixValue(objADGroup.AdsPath) ' use this for ADGroupName if you just want the ADS Path
  ADGroupName = Mid(FixValue(objADGroup.Name),4)
  GroupDescription = FixValue(objadgroup.Description)
  LastName = FixValue(objMember.sn)
  FirstName = FixValue(objMember.GivenName)
  Manager = FixValue(objMember.Manager)
  SamAccountName = FixValue(objMember.SamAccountName)
  Department = FixValue(ObjMember.Department)
  UserADSPath = FixValue(objMember.AdsPath)
qryInsert = "INSERT INTO [" & dbTableName & "](ADGroupName, GroupDescription, LastName, FirstName, ManagerAdsPath, SamAccountName, Department,CN, UserAdsPath) VALUES ('" & ADGroupName & "', '" & GroupDescription & "', '" & LastName & "', '" & FirstName & "',  '" & Manager & "', '" & SamAccountName & "', '" & Department & "', '" & CN & "', '" & UserAdsPath & "')"  
conn.Execute (qryInsert)
Next

Set objMember = Nothing
Set objRecordSet = Nothing
End Sub

conn.close
Set Conn = Nothing
MSgbox "Done"
Function FixValue(sValue)
FixValue = Replace(sValue,"'","''")
End Function
Sub WriteGroup(strGroupName)
WScript.Echo "WinNT://HEI/" & strGroupName
On Error Resume Next
Err.Clear
Set ObjGroup = GetObject("WinNT://HEI/" & strGroupName)
WScript.Echo Err.Number
If Err.Number <> 0 Then
strGroupName = mid(strGroupName,2)
WScript.echo strGroupName
Set ObjGroup = GetObject("WinNT://HEI/" & strGroupName)
End If
Err.Clear
On Error Goto 0

For Each objMember In objGroup.Members
   Set objUser = GetObject("WinNT://HEI/" & ObjMember.Name)
   ADGroupName = ObjGroup.Name
   SamAccountName = FixValue(ObjMember.Name)
   If UCase(objMember.Class) <> "GROUP" Then
    CN = FixValue(ObjMember.FullName)
   Else
    CN = "GroupWithinGroup"
   End If
   UserAdsPath = FixValue(ObjMember.AdsPath)
  
      qryInsert = "INSERT INTO [" & dbTableName & "](ADGroupName, GroupDescription, LastName, FirstName, ManagerAdsPath, SamAccountName, Department,CN, UserAdsPath) VALUES ('" & ADGroupName & "', '" & GroupDescription & "', '" & LastName & "', '" & FirstName & "', '" & Manager & "'. '" & SamAccountName & "','" & Department & "', '" & CN & "', '" & UserAdsPath & "')"
     
conn.Execute (qryInsert)
  Next
End Sub

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

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I haven't had the time to build a "write-back" yet with my MS Access stuff, but like you, i have a test lab and it's somehitng I'll need soon myself.  (Let me see if I can come up with something).  It takes a bit longer to write  because it's "writing" back and I tend to be a lot more careful because writing to AD with a script can be dangerous.  (Scripts Gone Wild!)

I do have an excel "write-back" script, but that only does User info, not group memberships.

Just an FYI - the script you just posted is a copy of an OLD version.  I updated it and started a new thread for it, be sure to use the new one I wrote because some of it had hard-coded stuff that only applied to my environment.  The link to the better version is here:  http://www.visualbasicscript.com/m_34962/mpage_1/key_/tm.htm#34962

(in reply to tcole33)
 
 
Post #: 26
 
 RE: Export Active Directory Users to Excel Worksheet - 6/27/2006 4:28:56 AM   
  DataBase

 

Posts: 3
Score: 0
Joined: 6/27/2006
Status: offline
man your script saved my life ... lol

really it's nice and simple.

is it possible to extract the passwords or the hashes so that i can add it later to our new AD server ?

we are making an upgrade for both Hardware and OS (from 2000 to 2003).

and I will use your tool to migrate the users from the old server on to another (I need a tool that will export to a file, then import from it).

Thank you so much for your work

(in reply to edavis6678)
 
 
Post #: 27
 
 RE: Export Active Directory Users to Excel Worksheet - 6/27/2006 5:15:18 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
I didnt write anything to extract passwords, best bet is to use a 3rd party hacking tool or just set everyone's password to 1 standard password and prompt user to change it on 1st login.

Just a note...my script is meant and designed to UPDATE active directory, not migrate.  (for example, my write-back script doesn't make new accounts, it just updates existing ones - so something to think about.

Check out the MS Access scripts I have though, it exports user group information which maybe helpful, but again, they aren't meant necessarily for migrations.

Glad you liked it.

(in reply to DataBase)
 
 
Post #: 28
 
 RE: Export Active Directory Users to Excel Worksheet - 6/27/2006 5:19:49 AM   
  DataBase

 

Posts: 3
Score: 0
Joined: 6/27/2006
Status: offline
quote:

ORIGINAL: edavis6678

I didnt write anything to extract passwords, best bet is to use a 3rd party hacking tool or just set everyone's password to 1 standard password and prompt user to change it on 1st login.

Just a note...my script is meant and designed to UPDATE active directory, not migrate.  (for example, my write-back script doesn't make new accounts, it just updates existing ones - so something to think about.

Check out the MS Access scripts I have though, it exports user group information which maybe helpful, but again, they aren't meant necessarily for migrations.

Glad you liked it.



so it will not work with a fresh installed AD server ?

(in reply to edavis6678)
 
 
Revisions: 1 | Post #: 29
 
 RE: Export Active Directory Users to Excel Worksheet - 6/27/2006 5:29:33 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
>>>   so it will not work with a fresh installed AD server ?

Nope.

What I'd recommend is using my script to extract your old AD info, then find another script that will create new users on your new AD server using the data that my script extracts.

However, that being said, for your situation I bet you can find a better tool that can migrate the user information better.  I know there are tons of Microsoft provided tools that enable you to script user creation, (and I bet there are a ton of scripts already posted here or on the net that can do what you exactly want).  I do have a script that CAN create new users, but it's highly embedded with my environment.  At some point I will try to integrate it with the scripts I've posted, but won't be some time since it's not every day you migrate your entire AD to a new AD

(in reply to DataBase)
 
 
Post #: 30
 
 RE: Export Active Directory Users to Excel Worksheet - 9/25/2006 5:16:57 PM   
  saifuddin

 

Posts: 1
Score: 0
Joined: 9/25/2006
Status: offline
This is the script i have been looking for. My requirement is a bit different though. I want to extract these details for a single user using the Alias. Any help will be appreciated. Thank you

(in reply to edavis6678)
 
 
Post #: 31
 
 RE: Export Active Directory Users to Excel Worksheet - 9/27/2006 2:05:21 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
quote:


want to extract these details for a single user using the Alias


I'm not sure what you mean by "alias" - what property are you referring to?

But I can show you some code to modify this to a single user.

(in reply to saifuddin)
 
 
Post #: 32
 
 RE: Export Active Directory Users to Excel Worksheet - 12/1/2006 4:46:20 AM   
  samola

 

Posts: 1
Score: 0
Joined: 12/1/2006
Status: offline
Hi,

I have a multi domain network and i want to just extract all the Domain Administrators from each domain. Would it be possible to get a script that will do this?

Thanks!

(in reply to edavis6678)
 
 
Post #: 33
 
 RE: Export Active Directory Users to Excel Worksheet - 12/1/2006 8:46:48 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
 
Samola:  I haven't had the time much lately with my new job to do much in AD scripting, however, with your question, I do have a script posted that dumps group membership to MS Access.  It's not exactly what you are asking, but it's close enough to get you your answer I think.

http://www.visualbasicscript.com/m_34962/mpage_1/key_/tm.htm#34962

The output would go to Access, but then coudl be filtered and exported to AD.


(in reply to samola)
 
 
Post #: 34
 
 RE: Export Active Directory Users to Excel Worksheet - 1/18/2007 9:38:16 AM   
  sedzen

 

Posts: 1
Score: 0
Joined: 1/18/2007
Status: offline
I just noticed that your import script doesn't support editing SMTP and SMTP alias fields. Do you or anyone else here have any scripts that they would suggest for bulk editing SMTP aliases? At the very least your export script has already saved me hours of work to help me with the info I need to make changes to my AD. So THANKS.

(in reply to edavis6678)
 
 
Post #: 35
 
 RE: Export Active Directory Users to Excel Worksheet - 2/5/2007 10:11:02 AM   
  seegoose

 

Posts: 2
Score: 0
Joined: 2/5/2007
Status: offline
OK  ... I am new and this is an older post, but I have been back and forth several times and I am pretty sure that no one has posted this error yet ...
Script error
Line: 125
Char: 31
Error: Expected end of statement
Code: 800A0401

This happens basically here -->
  Set objwb = objExcel <span class="high"> Active </span>Workbook.Worksheets("Sheet1")

Am I missing something?

Help? Please?


_____________________________

Me fail Engrish? That's Unpossible!

(in reply to edavis6678)
 
 
Post #: 36
 
 RE: Export Active Directory Users to Excel Worksheet - 2/5/2007 10:18:29 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
It sounds like you may have slightly modfied the script and didn't close an EndIf or Sub.

if I take my original script and find line 125, it's this line:

"Set objwb = objExcel.Workbooks.Add "

Which should NOT give that error.

(in reply to seegoose)
 
 
Post #: 37
 
 RE: Export Active Directory Users to Excel Worksheet - 7/9/2007 8:32:29 PM   
  kayman

 

Posts: 2
Score: 0
Joined: 7/9/2007
Status: offline
Mate this script is EXACTLY what i have been looking for, I just need one more feature... is there anyway you can list the groups the user belongs to as well? as in security or distribution groups?

also is there one site that lists all the object types / attributes available like ObjMember.CN ObjMember.XXXXX etc ?
I was trying to find the one that would give me the groups so I could try do it myself but couldnt.

Thanks SOOOO much.

< Message edited by kayman -- 7/9/2007 8:36:27 PM >

(in reply to edavis6678)
 
 
Revisions: 1 | Post #: 38
 
 RE: Export Active Directory Users to Excel Worksheet - 7/10/2007 3:14:14 AM   
  edavis6678

 

Posts: 121
Score: 0
Joined: 1/12/2006
Status: offline
Nice to hear that this was helpful.

Below is a link to another post I made that exports all group membership, (but not to Excel), but to MS Access.

http://www.visualbasicscript.com/m_34962/mpage_1/key_/tm.htm#34962

In addition to that, there is another script htere that does the same as this Excel export, but puts it in Access.

Hope this helps.

(in reply to kayman)
 
 
Revisions: 1 | Post #: 39
 
 RE: Export Active Directory Users to Excel Worksheet - 7/10/2007 10:46:30 AM   
  kayman

 

Posts: 2
Score: 0
Joined: 7/9/2007
Status: offline
Hey Edavis6678, thanks for the quick reply. I had a look at that script you have linked above and it works however the script you have written in this thread is exactly what i want, I just need it to list what groups they are a member off as well is that possible? (just like how it lists the other smtp address's

Rather then the other script which shows the groups and who its members are.

(in reply to edavis6678)
 
 
Post #: 40
 
 
Page:  <<   < prev  1 [2] 3   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Post a VBScript >> RE: Export Active Directory Users to Excel Worksheet Page: <<   < prev  1 [2] 3   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts