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!

Function Help

Status
Not open for further replies.

sparkbyte

Technical User
Sep 20, 2002
879
US
I found this script here from back on 05 and not sure why when converted to a function it does not set the result properly.

Could someone take a look and let me know what is wronge?

Thanks
John

Code:
strUserOU = GetUserOU(tuser)
WScript.Echo "User OU: " & strUserOU

Function GetUserOU(strUser)
Const ADS_SCOPE_SUBTREE = 2

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

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

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
Do Until objRecordSet.EOF
    strDN = objRecordSet.Fields("distinguishedName").Value
    arrPath = Split(strDN, ",")
    intLength = Len(arrPath(1))
    intNameLength = intLength - 3
    strUser = Right(arrPath(1), intNameLength)
    GetUserOU = Right(arrPath(1), intNameLength)
    objRecordSet.MoveNext
Loop
End Function
 
Replace this:
"SELECT distinguishedName FROM '" & strLDAPDomain &_
with this:
"SELECT distinguishedName FROM '" & strLDAPDomain & _

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Nope, same result.

If I "unwrap" the function and manually set the StrUser variable it work fine. I just can't get the function to properly return the lookup.

 
OK, found the issue.

strUserOU = GetUserOU("tuser")
WScript.Echo "User OU: " & strUserOU


tuser has to be in quotes.....

DEU error... (Defective end User)

Thanks
John
 
OK, new problem, same script.

I need to change the function so that it can adjust to the depth of the OU a user is in.

This works if the OU is off the root, but not if nested.
Not sure how to handle this.

IE.
user off root.
Accounting.mydomain.local
user OU under accounting
Payroll.accounting.mydomain.local

and the response need to be in LDAP form.
CN=Accounting,DC=mdltech,DC=local
CN=Payroll,CN=Accounting,DC=mdltech,DC=local

The idea is that I can then use this to pull a listing of the published printers in the users OU to connect to them.

Here is what I have.

Code:
Function GetUserLDAPOU(strUser)
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
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

Output said:
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

User OU: CN=Users,DC=mdltech,DC=local

***** script completed *****

And here is a start on the Published printer function.

Code:
Function GetOUPrinters(strUserOU)
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 printerName, serverName from " _     
    & " '" & strLDAPDomain & "'  where objectClass='printQueue'"  
Set objRecordSet = objCommand.Execute

'objRecordSet.MoveFirst
Do Until objRecordSet.EOF
			WScript.Echo "Found Printer: " & objRecordSet.Fields("printerName").Value
    objRecordSet.MoveNext
Loop
'	arrOUPrinters = strUser
End Function

the printers will be added to a dictionary to be used in the main script.

Thanks
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top