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!

Need ADSI Help 2

Status
Not open for further replies.

rbri

Programmer
Jun 27, 2002
84
US
Hello

I am trying to write a VBScript that extracts the account/userID from Active Directory and returns the useres real name. Through alot of searching and code experimenting I finally was able to get code to find and extract the users real name but when I get the user name in a variable I can't seem to do anything with it. For example I give the user an InputBox to enter a string I then attempt to cut the real name variable with the Split function into to single variables and the attempt to match the users input string with one of those variables. For some reason the split command will not work on the extracted Active Directory variable I pass to it. Is there something I am missing or should I be using a differnt command from split.
Here is my code.

Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objProvNt
Dim strDomain, strOrg, strOrgType, strOrgLoc, strProvider
Dim strNameVar, strVar, strChar, strLine, testvar
Dim astrFullName()
Dim intStringLen, intIndex, intStart, intFlag

'strUserName = "(my user ID)"
strProvider = "WinNT://"
strDomain = "(my domain)"
strOrg = "(my organization)"
strOrgType = "net"
strOrgLoc = "(my organizations location)"
intFlag = 0

If intFlag = 0 Then
strNameVar = InputBox("Enter your name")
Set objProvNt = GetObject(strProvider & strDomain)
objProvNt.Filter = Array("User")
For Each strLine In objProvNt
On Error Resume Next
strVar = strLine.FullName
intStringLen = Len(strVar)
intStart = 1
For intIndex = 1 To intStringLen Step 1
strChar = Mid(strVar, intStart, 1)
If strChar = "," Then
strVar = LTrim(strVar)
strVar = RTrim(strVar)
astrFullName = Split(strVar, ",")

' MsgBox "Variable before array split " & strVar
' MsgBox "Variable to search for " & strNameVar
MsgBox "First array element " & astrFullName(0)
MsgBox "Second array element " & astrFullName(1)


' If astrFullName(0) = strNameVar Then
' MsgBox "You found your person " & strVar
' ElseIf astrFullName(1) = strNameVar Then
' MsgBox "You foung your person " & strVar
' Else
' MsgBox "No one by that name was found"
' intFlag = 1
' End If
End If
intStart = intStart + 1
Next
Next
Else
MsgBox "All Done"
End If
 
Can you give an example of strVar value before splitting (just after the line "strVar = strLine.FullName" ? Water is not bad as long as it stays out human body ;-)
 
Sure

The variable returned looks like this.

(LastName), (FirstName) ie Doe, John

The reason I loop through each character is because some of the variables returned are not real people names for example some look like registy entries others look like _AHDJR or _Site 12 but everyones real name looks like the example above. The LTrim and RTrim functions where only my experiment they did not seem to help.

Thanks
 
can u clarify.

the users input string is a name or part of a name?

you're trying to match this with a name in the active database?

is this what you are trying to do?


===============
Security Forums
 
Yes the users Input string in most likely a part of a name but it could be the whole name. Yes I am trying to match it to a name in the Active Directory.

Thanks
 
What I should do :
Code:
If intFlag = 0 Then
 strNameVar = InputBox("Enter your name")
 Set objProvNt = GetObject(strProvider & strDomain)
 objProvNt.Filter = Array("User")
 For Each strLine In objProvNt
  On Error Resume Next
  strVar = strLine.FullName
  'Detecting the comma that indicates a real Name
  if instr(strVar, ",") > 0 then
    'removing leading and trailing blanks
    strVar = Trim(strVar)
    astrFullName = Split(strVar, ",")
    MsgBox "First array element  " & astrFullName(0)
    MsgBox "Second array element " & astrFullName(1)
   End If
 Next
Else
 MsgBox "All Done"
End If
Water is not bad as long as it stays out human body ;-)
 
Correction : to be sure, let's ignore case. The code now becomes :
Code:
If intFlag = 0 Then
 strNameVar =
ucase(
Code:
InputBox("Enter your name")
)
Code:
 Set objProvNt = GetObject(strProvider & strDomain)
 objProvNt.Filter = Array("User")
 For Each strLine In objProvNt
  On Error Resume Next
  strVar = strLine.FullName
  'Detecting the comma that indicates a real Name
  if instr(strVar, ",") > 0 then
    'removing leading and trailing blanks
    strVar =
ucase(
Code:
Trim(strVar)
)
Code:
    astrFullName = Split(strVar, ",")
    MsgBox "First array element  " & astrFullName(0)
    MsgBox "Second array element " & astrFullName(1)
   End If
 Next
Else
 MsgBox "All Done"
End If
Water is not bad as long as it stays out human body ;-)
 
I'am confused for some reason neither of the array elements or the text in the message box print. the only way I get anything is to print the variable strVar which holds the users full real name. I know the (,) check is working because if I put a MsgBox with strVar inside the If loop it print the users real name to the screen all in capital letters.
 
try that and tel me what it gives :
If intFlag = 0 Then
strNameVar = ucase(InputBox("Enter your name"))
Set objProvNt = GetObject(strProvider & strDomain)
objProvNt.Filter = Array("User")
For Each strLine In objProvNt
On Error Resume Next
strVar = strLine.FullName
'Detecting the comma that indicates a real Name
if instr(strVar, ",") > 0 then
'removing leading and trailing blanks
strVar = ucase(Trim(strVar))
astrFullName = Split(strVar, ",")
MsgBox strVar & " has been cut into " & (ubound(astrFullName) -1) & " elements"

MsgBox "First array element " & astrFullName(0)
MsgBox "Second array element " & astrFullName(1)
End If
Next
Else
MsgBox "All Done"
End If
Water is not bad as long as it stays out human body ;-)
 
i would do:

excuse my pseudo code.

namefound = false
For Each strLine In objProvNt

strVar = strLine.FullName
temp = split(strvar,",")
name = temp(1) & " " & temp(0)

if instr(name,inputstring) > 0 then
namefound = true
exit for
end if
next
if namefound then msgbox found strvar
else
msgbox notfound strvar
end if

at this p

tho i just realised if there's more than one possible match then it wouldnt pickup more than one.


===============
Security Forums
 
I don't know what it is but when ever I try to use the array I get nothing no text message, array element or error it just gives me nothing. However if is MsgBox strVar I get the users real name in the context of (LastName, FirstName) every time. Now isn't that a mystery.
 
Did you put an "Option Explicit" statment at the top of your vbs ? If no, add it. It forces you to declare all variables (with dim). If yes, send the full code (with dims). I think that could be an error on var name or something like that. Water is not bad as long as it stays out human body ;-)
 
OK Here is the code:

'Program Function:
'**Start Encode**
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objProvNt
Dim strDomain, strOrg, strOrgType, strOrgLoc, strProvider
Dim strNameVar, strVar, strLine
Dim astrFullName()
Dim intFlag

'strUserName = "(myLoginID)"
strProvider = "WinNT://"
strDomain = "(myDomain)"
strOrg = "(myORganization)"
strOrgType = "net"
strOrgLoc = "(myOrganizationLocation)"
intFlag = 0

If intFlag = 0 Then
strNameVar = InputBox("Enter your name")
Set objProvNt = GetObject(strProvider & strDomain)
objProvNt.Filter = Array("User")
For Each strLine In objProvNt
On Error Resume Next
strVar = strLine.FullName

'Detecting the comma that indicates a real Name
If instr(strVar, ",") > 0 then
'removing leading and trailing blanks
strVar = ucase(Trim(strVar))
astrFullName = Split(strVar, ",")
MsgBox strVar
MsgBox strVar & " has been cut into " & (ubound(astrFullName) -1) & " elements"
MsgBox "First array element " & astrFullName(0)
MsgBox "Second array element " & astrFullName(1)
End If
Next
Else
MsgBox "All Done"
End If
 
I think I found : remove the parenthesis on the declaration of "Dim astrFullName()". I know, Split function returns an array but you must provide a single Variant var... That's Mr Bill Gates logic [hammer] !!!
Water is not bad as long as it stays out human body ;-)
 
Thanks that worked both array elements are now displaying in the MsgBox.
 
I had to leave my desk for a moment. Yes I thank everyone for the help it is greatly appreiated. I was also wondering is there a better way for me to query the Active Directory for user names. The way I am doing it does take a bit of time. I did create a text file with all the real names in it and it contains over 36000 entries. Again I say to everyone thank you very much for the help.

Thanks Randy
 
By the way, I once use a script to get userName from network login. Try it to see if it works for you :

Code:
'Get environment
Set Gob_WshEnv = Gob_WshShell.Environment("PROCESS")
'Get Current user from its domain.
Set G_User = GetObject("WinNT://" & Gob_WshEnv.Item("USERDOMAIN")& "/" & WshNetwork.UserName)
msgbox G_User.Name

I think that could be a shorter way to get user login without asking him to type anything.

Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top