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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP - need function to return full path to user OU 1

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I have a script that finds printers that have been published in AD and placed in the same OU as the user and connects to them, then reads the users group memberships and assigns their default printer.

Works great if the OU if off root. Need this function to return the users full LDAP path of the OU his user object resides in.

Thanks

Code:
Function GetUserLDAPOU(strUser)
Dim objRecordSet
Const ADS_SCOPE_SUBTREE = 2

'Automatically find the domain name
Dim objDomain, objLDAPDomain, strDomain, strLDAPDomain
Set objDomain = getObject("LDAP://rootDse")
strDomain = objDomain.Get("dnsHostName")
strLDAPDomain = "LDAP://" & objDomain.Get("defaultNamingContext")
Set objLDAPDomain = GetObject(strLDAPDomain)

Dim objConnection, objCommand
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = _
    "SELECT distinguishedName FROM '" & strLDAPDomain & _ 
     "'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute

'objRecordSet.MoveFirst
Dim strDN, arrPath, intLength, intNameLength
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName").Value
    arrPath = Split(strDN, ",")
    intLength = Len(arrPath(1))
    intNameLength = intLength - 3
'    strUser = Right(arrPath(1), intNameLength)
    strUser = arrPath(1) & "," & arrPath(2) & "," & arrPath(3)
    objRecordSet.MoveNext
Loop
	GetUserLDAPOU = strUser
End Function 


Dim WshNetwork, strUserName
Set WshNetwork = WScript.CreateObject("WScript.Network")
strUserName = WshNetwork.UserName

Dim strUserLDAPOU
strUserLDAPOU = GetUserLDAPOU(strUserName)
WScript.Echo strUserLDAPOU


Thanks

John Fuhrman
Titan Global Services
faq329-6766
 
Maybe this could be adapted to do users instead of computers?

posted by PScottC
Code:
Dim wshShell, wshNetwork
Dim strComputerName

' Create Global Objects
Set wshShell = CreateObject("WScript.Shell")
Set wshNetwork = CreateObject("WScript.Network")
' Initialize Variables
strComputerName = wshNetwork.ComputerName
'wscript.echo "Computer DN: " & GetDN


Function GetDN()
' Use the NameTranslate object to convert the NT name of the computer to
' the Distinguished name required for the LDAP provider. Computer names
' must end with "$". Returns comma delimited string to calling code.
Dim objTrans, objDomain
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
Set objTrans = CreateObject("NameTranslate")
Set objDomain = getObject("LDAP://rootDse")
objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, wshNetwork.UserDomain & "\" _
& strComputerName & "$"
GetDN = objTrans.Get(ADS_NAME_TYPE_1779)
'Set DN to upper Case
GetDN = UCase(GetDN)
End Function

wscript.echo "Computer DN: " & GetDN

Thanks

John Fuhrman
Titan Global Services
faq329-6766
 
This is what I wrote to return the distinguished name (DN) of various AD objects:

Code:
Function getDN(category,aDQ,var1)
	Set oRootDSE = GetObject("LDAP://rootDSE")
	Set oItem = GetObject("LDAP://" & oRootDSE.Get("defaultNamingContext"))
	
	Const ADS_SCOPE_SUBTREE = 2
	
	Set oConnection = CreateObject("ADODB.Connection")
	Set oCommand =   CreateObject("ADODB.Command")
	oConnection.Provider = "ADsDSOObject"
	oConnection.Open "Active Directory Provider"
	Set oCommand.ActiveConnection = oConnection
	
	oCommand.Properties("Page Size") = 1000
	oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
	If aDQ = "sAMAccountName" Then
		oCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & _
		oRootDSE.Get("defaultNamingContext") & "' WHERE objectCategory='" &_
		 category &"' AND sAMAccountName='"& var1 &"'"
		
	ElseIf aDQ = "displayName" Then
		oCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & _
		oRootDSE.Get("defaultNamingContext") & "' WHERE objectCategory='"&_
		 category &"' AND displayName='"& var1 &"'"
		
	ElseIf aDQ = "canonicalName" Then
		'MsgBox oRootDSE.Get("defaultNamingContext")
		oCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & _
		oRootDSE.Get("defaultNamingContext") & "' WHERE objectCategory='"&_
		category &"' AND name='"& var1 &"'"
	Else
		MsgBox "Please define the Active Directory Query Type (aDQ)."
	End If
	
	Set oRecordSet = oCommand.Execute
	oRecordSet.MoveFirst
	
	Do Until oRecordSet.EOF
	    strCompleteDN = oRecordSet.Fields("distinguishedName").Value
	    getDN = strCompleteDN
	    oRecordSet.MoveNext
	Loop
End Function

Some examples of its use is:

Code:
'Return DN of user's Full Name (Display Name)
getDN("user","displayName","Firstname Surname")

'Return DN of OU
getDN("organizationalUnit","canonicalName","Name of an OU")

'Return DN of group (I use for security groups)
getDN("group","sAMAccountName","Group Name")

'Return DN of group by displayName (I use this
'for Distribution groups)
getDN("group","displayName","Group Name")

'Return username from displayName
getDN("user","displayName","FirstName SurName")

Don't forget you may need to add "LDAP://". It should automatically pick up your domain name.

If anyone would like to comment on improving it or posting a link to the MSDN page that lists all the AD objects and groups, like sAMAcountName, group, user, displayName etc, I'd be grateful.

Hope this helps.

Woter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top