Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

markdmac, script question

Status
Not open for further replies.

wdoellefeld

Technical User
May 3, 2004
492
US
Hey Mark,

Can you point me to a script that will allow me to find what OU a user resides in? I was hoping for something that will pop up a dialog and we could use the sam account name. I'd like to share it with our team here.

Thanks!

FRCP
 
I'm no Mark, but here is where I usually start for AD script examples.


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Are you just looking for the OU or the full OU path?

I hope you find this post helpful.

Regards,

Mark
 
You could grab the Distinguished name, split that on the commas and get the OU name from that.

Here is an example:
Code:
dn= "cn=tom thumb,ou=techdudes,cn=users,dc=company,dc=local"
MyArray = Split(dn,",")
OU = MyArray(1)
WScript.Echo OU

So basically all you need is to grab the DistinguishedName.
Refer to faq329-5688 for an example of how to do that.

I hope you find this post helpful.

Regards,

Mark
 
Here is what I ended up with..

Code:
On Error Resume Next
Err.clear
Set objNetwork = CreateObject("WScript.Network")

domain = "YOURDOMAIN"

user = inputbox("Please enter a Username", "Input" )

Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_PROPERTY_APPEND = 3

Set objTrans = CreateObject("NameTranslate")
objTrans.Init ADS_NAME_INITTYPE_GC, ""

' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, domain & "\" & user

If err.number <> 0 then
	ws.cells(i,6).value = "User does not exist"
	errcounter = errcounter + 1
	err.clear
Else

	' Use the Get method to retrieve the RPC 1779 Distinguished Name.
	strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

	' Bind to the user object in Active Directory with the LDAP provider.
	Set objUser = GetObject("LDAP://" & strUserDN)

	Set objOU = GetObject(objUser.Parent)
	strOU = Replace(objOU.Name, "OU=", "")
	msgbox "Username: " & Ucase(user) & vbcrlf & vbcrlf & "OU: " & strOU
			
	user = ""
	strUserDN = ""	
	
	set objUser = nothing
	
End If

set ObjTrans = nothing
	
set ObjUser = nothing
set xlApp = nothing
set wb = nothing
set ws = nothing

wscript.quit

Seems to work great.

FRCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top