Solved

Creating multiple worksheets using AD data

Posted on 2009-04-01
8
464 Views
Last Modified: 2012-05-06
Looking for a script that will run through an OU extract the users and place them on separate worksheets within a single workbook based on department name.
0
Comment
Question by:WaywardS0n
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 24056191
Hi there.

This will hopefully do the job for you, so far without saving or closing the resultant Excel file.

If you want that done, let me know.

For this to work for your particular OU, you'll need to set this line
Set objOU = GetObject("LDAP://OU=TestUsers,OU=TestOU," & strDNSDomain)

to match your OU path, in reverse order.  For example, the above refers to
domain.com/TestOU/TestUsers

Regards,

Rob.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objOU = GetObject("LDAP://OU=TestUsers,OU=TestOU," & strDNSDomain)
objOU.Filter = Array("User")
Set objList = CreateObject("Scripting.Dictionary")
For Each objUser In objOU
	strDept = UCase(objUser.Department)
	If objList.Exists(strDept) = True Then
		objList(strDept) = objList(strDept) & ";" & objUser.samAccountName
	Else
		objList.Add strDept, objUser.samAccountName
	End If
Next
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Const xlUp = -4162
Set objWB = objExcel.Workbooks.Add
While objWB.Sheets.Count > 1
	objWB.Sheets(objWB.Sheets.Count).Delete
Wend
For Each strDept In objList
	If objWB.Sheets(1).Name = "Sheet1" Then objWB.Sheets(1).Name = strDept
	boolSheetFound = False
	For intSheet = 1 To objWB.Sheets.Count
		If objWB.Sheets(intSheet).Name = strDept Then
			boolSheetFound = True
			Exit For
		End If
	Next
	If boolSheetFound = False Then
		objWB.Sheets.Add , objWB.Sheets(objWB.Sheets.Count)
		objWB.Sheets(objWB.Sheets.Count).Name = strDept
	End If
	intLastRow = objWB.Sheets(strDept).Cells(65536, 1).End(xlUp).Row
	If intLastRow = 1 Then
		objWB.Sheets(strDept).Cells(intLastRow, 1).Value = "Username"		
	End If
	arrUsers = Split(objList(strDept), ";")
	For Each strUser In arrUsers
		intLastRow = intLastRow + 1
		objWB.Sheets(strDept).Cells(intLastRow, 1).Value = strUser
	Next
Next

Open in new window

0
 

Author Comment

by:WaywardS0n
ID: 24060738
The problem I'm having is it appears objOU is returning an empty collection.
I'm sure the ldap path is correct as I use it in a miriad of other scripts.
(w,x,y,z are used for obscurity)

--------------------------------------------------------------------
Set objOU = GetObject("LDAP://ou=WAMC,ou=NARMC,dc=wwww,dc=xx,dc=yyyy,dc=zzz")
objOU.Filter = Array("user")
wscript.echo objOU.count
Set objList = CreateObject("Scripting.Dictionary")
For Each objUser In objOU
  strDept = UCase(objUser.Department)
  wscript.echo strDept
--------------------------------------------------------------------
I'm using the 1st wscript.echo  to try to show number of items returned by the query.
that wscript.echo line returns an error 8004001 error: Not Implemented.

The 2nd wscript.echo is used to see if I actually have a value in strDept. The script never sees that line. I'm still working with it.
Any help would be appreciated.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24065268
Hmmm, OK, without filtering the OU then, let's see what "class" of objects are in there....

Set objOU = GetObject("LDAP://ou=WAMC,ou=NARMC,dc=wwww,dc=xx,dc=yyyy,dc=zzz")
objOU.Filter = Array("user")
For Each objUser In objOU
   WScript.Echo objUser.Name & " - " & objUser.Class
Next

I've never had much success with the Count property. I just loop through it an increment a manual counter....

Regards,

Rob.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 24065271
Oh, I wasn't meant to include the filter.....

Set objOU = GetObject("LDAP://ou=WAMC,ou=NARMC,dc=wwww,dc=xx,dc=yyyy,dc=zzz")
For Each objUser In objOU
   WScript.Echo objUser.Name & " - " & objUser.Class
Next

Regards,

Rob.
0
 

Author Comment

by:WaywardS0n
ID: 24077521
Thank you for your assistance. While I had to tweak the code, it did the tricks. Another outstanding job.. Thanks...
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24082443
No problem. Thanks for the grade.

So did you end up having to use
If objUser.Class = "user" Then

??

Regards,

Rob.
0
 

Author Comment

by:WaywardS0n
ID: 24082582
Actually I just made a SQL call through LDAP to AD (instead of the getobject) and then fed the record set into the scripting dictionary and your code did the rest! By using SQL I was able to filter and sort before feeding the data into the dictionary. I'm sure your way works great and I'm not saying my way is better. Sometimes we have to go with what we are familiar with. I was stuck on a clean way to add, rename and populate the individual sheets. The method you laid out was simple, clean and efficient.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 24082647
LOL!  No problem at all!  Good thinking!

Regards,

Rob.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question