Querying Active Directory

I am trying to get my script to return details from Active Directory based on the currently logged in user, I have the script to query AD and it works great and gives me every user, how can I restrict the search to provide just the logged in user?

here is what I have

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

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

objCommand.CommandText = _
 "<LDAP://ou=users,dc=server,dc=comany,dc=world,dc=com>;(objectCategory=user)" _
  & ";name,sAMAccountName;subtree"
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
 Wscript.Echo objRecordSet.Fields("name")
 Wscript.Echo objRecordSet.Fields("sAMAccountName")
 objRecordSet.MoveNext
Wend

objConnection.Close
LVL 2
nemesis1210Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
Function NameOfUser() As String
NameOfUser = Environ("UserName")
End Function

Will get you the Current user so loop till you find it o
0
nemesis1210Author Commented:
Where in the script do I put it and how do I tell it to loop till it finds it?
0
Mikal613Commented:
While Not objRecordSet.EOF
   IF  objRecordSet.Fields("name") = NameOfUser then
 
        Wscript.Echo objRecordSet.Fields("name")
        Wscript.Echo objRecordSet.Fields("sAMAccountName")
   end if
 objRecordSet.MoveNext
Wend
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nemesis1210Author Commented:
it did not like your function command, is it a VB or VB Script function had that problem already I need VB Script

0
Mikal613Commented:

Set wshShell = CreateObject("WScript.Shell")
strUser = wshShell.ExpandEnvironmentStrings("%USERNAME%")
WScript.Echo "Current User: " & strUser

 IF  objRecordSet.Fields("name") = strUser then
 
        Wscript.Echo objRecordSet.Fields("name")
        Wscript.Echo objRecordSet.Fields("sAMAccountName")
   end if

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nemesis1210Author Commented:
I managed to get it to work with

Dim strUserName

getusername
Sub GetUserName()
  Dim WSHNetwork  

  Set WSHNetwork = CreateObject("WScript.Network")
  strUserName = WSHNetwork.username
  msgbox strusername
End Sub

however you are the only person to help I am sure your method will work also so the points are yours

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.