/build/static/layout/Breadcrumb_cap_w.png

Script to find users in AD and return values

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

Answers (12)

Posted by: brenthunter2005 19 years ago
Posted by: Naffcat 19 years ago
Senior Purple Belt
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
Posted by: brenthunter2005 19 years ago
Fifth Degree Brown Belt
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
Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
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.
Posted by: Naffcat 19 years ago
Senior Purple Belt
0
Thanks, tried that but afraid it didn't work
Posted by: Naffcat 19 years ago
Senior Purple Belt
0
No I haven't I'll take a look - many thanks
Posted by: Naffcat 19 years ago
Senior Purple Belt
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?
Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
0
You're right.

Try here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en
Posted by: Naffcat 19 years ago
Senior Purple Belt
0
Many thanks that worked
Posted by: Naffcat 19 years ago
Senior Purple Belt
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?
Posted by: brenthunter2005 19 years ago
Fifth Degree Brown Belt
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
Posted by: WiseUser 19 years ago
Fourth Degree Brown Belt
0
Sorry Naffcat - my mistake!!![:)]

Well spotted Brent!
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ