Has anyone got some vb script code to find a particular user in AD and return for example the username, container and displayname?
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Answered 02/22/2005 by: brenthunter2005
Fifth Degree Brown Belt

Please log in to comment
0
Thanks Brent, using the MS site I managed to put together this, it reads a list of usernames from a text file and returns the display name. However I would like it to return the description also but when I add in the **** line the script fails. I think it is because the description can be more than one line.

Can you help with the final part?


Dim objFSO, objReadQuery, objConnection, objCommand, objRecordset
Dim strMachine, strUsername, strDispname, strDesc

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objreadQuery = objFSO.OpenTextFile("C:\Temp\testread.txt", 1)
objreadQuery.SkipLine

Do until objreadQuery.AtEndOfStream
objreadQuery.Skip (0) 'machine name starting char position
strMachine = objreadQuery.Read(9)
objreadQuery.Skip (121) 'anumber starting char position
strUserName = objreadQuery.Read(7)
Call searchAD
WScript.Echo strMachine & " " & strUsername & " " & strDispname & " " & strDesc


objreadQuery.SkipLine
Loop

Sub searchAD
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
"<LDAP://ou=UK,dc=Xdo,dc=Xdomain,dc=com>;(&(objectCategory=User)" & _
"(samAccountName=" & strUserName & "));samAccountName,displayName,description;subtree"

Set objRecordSet = objCommand.Execute

If objRecordset.RecordCount = 0 Then
strUsername = "No last logon user"
strDispname = ""
Else
strUsername = objRecordset.Fields("samAccountName")
strDispname = objRecordset.Fields("displayName")
'strDesc = objRecordset.Fields("description") **** ?

End If

End Sub

objConnection.Close
Answered 02/24/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
Shouldn't your code include the .Value added on the end?

Therefore:

Shouldn't the following line 'strDesc = objRecordset.Fields("description") **** ? look like this 'strDesc = objRecordset.Fields("description").Value
Answered 02/24/2005 by: brenthunter2005
Fifth Degree Brown Belt

Please log in to comment
0
Have you seen the ADSI Scriptomatic yet?

http://www.microsoft.com/technet/scriptcenter/tools/admatic.mspx

It's a godsend when it comes to ADSI scripting.
Answered 02/24/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
Thanks, tried that but afraid it didn't work
Answered 02/25/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
No I haven't I'll take a look - many thanks
Answered 02/25/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
I can't seen to find the link to download it - there is a line which says click here to download but it doesn't actually do anything ... have you seen this?
Answered 02/25/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
You're right.

Try here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en
Answered 02/26/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
0
Many thanks that worked
Answered 02/26/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
Being new to this - could you point me in the right direction to the AD stuff, which WMI namespace and classes should I take a look at?
Answered 02/26/2005 by: Naffcat
Senior Purple Belt

Please log in to comment
0
Sounds like you downloaded Scriptomatic 2.0 instead of the ADSI Scriptomatic.
The correct one is here http://www.microsoft.com/downloads/details.aspx?FamilyID=39044e17-2490-487d-9a92-ce5dcd311228&DisplayLang=en
Answered 02/28/2005 by: brenthunter2005
Fifth Degree Brown Belt

Please log in to comment
0
Sorry Naffcat - my mistake!!![:)]

Well spotted Brent!
Answered 02/28/2005 by: WiseUser
Fourth Degree Brown Belt

Please log in to comment
Answer this question or Comment on this question for clarity