'==========================================================================
'
' NAME: <FindUserOU.vbs>
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE : 6/25/2005
'
' COMMENT: Thanks go out to Tek-Tips user K0b3 for the
' SearchDistinguishedName function.
'
'==========================================================================
' Code assumes you have already grabbed the user login name as UserString
ON ERROR RESUME NEXT
Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
'First grab the DistinguishedName
UserDN = SearchDistinguishedName(UserString)
'Now bind to the user object
Set UserObj = GetObject("LDAP://" & UserDN)
'Find the Relative Distinguished Name (RDN)
UserRDN = UserObj.Name
'Subtract the length of the RDN plus one more for the comma
'You now have the full path to the users OU
UserOU = Right(UserDN,Len(UserDN)-Len(UserRDN)-1)
'WScript.Echo UserOU
'WScript.Echo UserRDN
[red]
'----------------------------------------------------------
' Write Organizational information into user object.
' Added to script 11-10-2006
' John Fuhrman
'----------------------------------------------------------
UserObj.Put "title", "System Administrator"
UserObj.Put "department", "Outlink Data Center Management Team"
UserObj.Put "company", "Jack Henry & Assoc."
UserObj.SetInfo
[/red]
'-----------------------------------------------------------------------------------
Public Function SearchDistinguishedName(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 & "));distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
On Error Resume Next
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
On Error GoTo 0
oConnection.Close
Set oRecordSet = Nothing
Set oCommand = Nothing
Set oConnection = Nothing
Set oRootDSE = Nothing
End Function