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)
Please log in to answer
Posted by:
Naffcat
19 years ago
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
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
Posted by:
WiseUser
19 years ago
Posted by:
Naffcat
19 years ago
Posted by:
WiseUser
19 years ago
Posted by:
Naffcat
19 years ago
Posted by:
brenthunter2005
19 years ago
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
The correct one is here http://www.microsoft.com/downloads/details.aspx?FamilyID=39044e17-2490-487d-9a92-ce5dcd311228&DisplayLang=en
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.