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
Thanks
John Fuhrman
Titan Global Services
faq329-6766
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