Solved

Querying Active Directory

Posted on 2004-10-29
185 Views
Last Modified: 2010-05-02
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
0
Question by:nemesis1210
    6 Comments
     
    LVL 48

    Expert Comment

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

    Will get you the Current user so loop till you find it o
    0
     
    LVL 2

    Author Comment

    by:nemesis1210
    Where in the script do I put it and how do I tell it to loop till it finds it?
    0
     
    LVL 48

    Expert Comment

    by:Mikal613
    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
     
    LVL 2

    Author Comment

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

    0
     
    LVL 48

    Accepted Solution

    by:

    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
     
    LVL 2

    Author Comment

    by:nemesis1210
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now