• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

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
0
nemesis1210
Asked:
nemesis1210
  • 3
  • 3
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now