Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Finding a User's OU

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Finding a User's OU
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Finding a User's OU - 6/8/2007 12:44:58 AM   
  RobertParker

 

Posts: 24
Score: 2
Joined: 6/8/2007
Status: offline
Hey I'm pretty new to VBScript.  They gave me a book 1.5 weeks ago at work, and told me I had to learn it.  Anyway part of the script that I'm working on right now is a login script.  The part I'm having trouble with is finding the User's OU.  The scripting guys have a script for doing this here. Use the first script on the page. http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1021.mspx

But when I run the script instead of getting a distinguished name like: CN=kenmyer,OU=Finance,DC=fabrikam,DC=com
mine looks more like: CN=RobertParker,OU=Users,OU=Location1,OU=HUB,OU=US,DC=xxxxxx,DC=xxxxx

I think the OUs are set as follows:  Location1 is a subgroup of HUB, which is a subgroup of US, which is a subgroup of USERS
The only one I want is the one is the lowest level group: OU=Location1

I started writing some code to try and filter out the wrong OUs, but as our OU setup is very complicated it would be very sloppy to program and any update to AD to could cause the code to break.  I stopped doing that and now I am stuck.  I'm also pretty new to AD btw.

How can I find the lowest OU that a user is in?
 
 
Post #: 1
 
 RE: Finding a User's OU - 6/8/2007 2:31:42 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Assuming that you have already gottent eh distinguishedname into a string called sDN, you can use the following:
WScript.Echo Split(sDN,",OU=")(1)

EDIT: I was stupid the first time, changed the split characters to be ,OU= instead of OU=.  This way i don't have to do a replace to get rid of the commas.  *sigh*  Dumb me.

< Message edited by DiGiTAL.SkReAM -- 6/8/2007 5:03:06 AM >


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to RobertParker)
 
 
Post #: 2
 
 RE: Finding a User's OU - 6/8/2007 2:41:25 AM   
  RobertParker

 

Posts: 24
Score: 2
Joined: 6/8/2007
Status: offline
yeah I thought about doing something similar, but not as elegant as your one-liner.

The problem is that doing it that way hardcodes it to my distinguished name format.  Sure it would work on my computer/user, but what if another user was fewer subgroups or more subgroups then it would break.

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 3
 
 RE: Finding a User's OU - 6/8/2007 5:00:03 AM   
  DiGiTAL.SkReAM


Posts: 1194
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
The only time it would break is if there were NO OU='s in the distinguishedname.  for example:
CN=Username,OU=Users,DC=test,DC=com
CN=Username,OU=Users,OU=Test1,OU=Test2,OU=Test1,OU=Test2,OU=Test1,OU=Test2,OU=Test1,OU=Test2,DC=test,DC=com

using the code I gave you, both of the above would break down to
Users

But if it were
CN=Username,DC=test,DC=com

The code would break, yes.  BUT, if you are having usernames in your ROOTDSE, then you have more problems than trying to find their OU....


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to RobertParker)
 
 
Post #: 4
 
 RE: Finding a User's OU - 6/10/2007 11:56:52 PM   
  RobertParker

 

Posts: 24
Score: 2
Joined: 6/8/2007
Status: offline
I found an alternative this morning.  Taking a break for the weekend makes it all clearer.  The original code was to check if a user is in a certain OU, I was trying to get the distinguished name, then get the right OU, then compare the two strings.  Instead I can search if the OU string exists in the distinguishedName string.

I spent half of Friday trying to figure this out, and I did this in 5 minutes this morning.

If InStr(distinguishName,"theOU") > 0

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 5
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Finding a User's OU Page: [1]
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