Link to home
Start Free TrialLog in
Avatar of supportemea
supportemea

asked on

VBScript to Return OU where User account is located

Hey Guys,

Im trying to create a generic login script for a domain that has about 5 OU's of where users are located.

Im having a hard time finding a script that will return the OU of where a user account is located
Avatar of EYRajeshTV
EYRajeshTV
Flag of India image

This will give the OU location when user account is given
================================================

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
FindUser = InputBox("Please Enter the UserName", "Find User OU")
If FindUser = "" Then
MsgBox("No UserName Was Added")
WScript.Quit
Else
strUser = FindUser
End If
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
    "SELECT ADsPath FROM 'LDAP://dc="giveurdchere",dc="yourdc",dc="yourdc"'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("ADsPath").Value
    objRecordSet.MoveNext
Loop
===================================================
Avatar of RobSampson
Hi there, for a logon script, this will give you the breakdown of the path for your current user account, so you can pick which element you are after and change the index in
strOU = arrOU(1)

to suit.

Regards,

Rob.
Set objADSysInfo = CreateObject("ADSystemInfo")
arrOU = Split(objADSysInfo.UserName, ",")
strPath = "OU Path"
For intBit = LBound(arrOU) To UBound(arrOU)
	strPath = strPath & VbCrLf & "Element " & intBit & ": " & arrOU(intBit)
Next
MsgBox strPath
strOU = arrOU(1)
MsgBox strOU

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrfixit584
mrfixit584

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial