VB script to query AD

J

Jason

I am looking to create a script that will check against AD
the sAMAccountName and return the Name of a user who is
logged into a workstation.

Any help would be appreciated.
 
L

Laura E. Hunter \(MVP\)

See if "psLoggedOn" will do what you need. It's part of the ps-*.* freeware
from Sysinternals, which is the best stuff on earth.
 
J

Jason

That tool works but not quite what I am looking for. I
just want to be able to run a query against a remote
machine and use the id that I am retrieving from that
machine to pass as a variable in a vbscript to query
against Active Directory. In that query I am looking to
find the user.Name property that corresponds to the
user.sAMAccountName. The user.name property is the ID of
the user that I will have retrieved from the remote
machine.

Any other thoughts.
 
H

Hunter Coleman

This will do it when run from the local machine. To get it to work against a
remote machine will take a bit of tweaking, but this may get you started.
Watch for line wraps....

Hunter

==================================================================
set objComputer = CreateObject("Wscript.Network")
strUserName = objComputer.UserName
set objComputer = Nothing
wscript.echo strUserName

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADSDSOObject"
adoConnection.Open "", "", ""

Set RootDSE = GetObject("LDAP://RootDSE")
searchRoot = RootDSE.Get("defaultNamingContext")

'On Error Resume Next
Set adoRecordset = adoConnection.Execute _
("<LDAP://" & searchRoot & ">;(&(objectCategory=Person)(sAMAccountName=" &
strUserName & "));" _
& "Name,ADsPath,sAMAccountName;SubTree")

While Not adoRecordset.EOF
wscript.echo adoRecordset.Fields.Item("Name").Value
adoRecordset.MoveNext
Wend
===============================================================
 
J

Jason

Thanks. That works great.

-----Original Message-----
This will do it when run from the local machine. To get it to work against a
remote machine will take a bit of tweaking, but this may get you started.
Watch for line wraps....

Hunter

========================================================== ========
set objComputer = CreateObject("Wscript.Network")
strUserName = objComputer.UserName
set objComputer = Nothing
wscript.echo strUserName

Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADSDSOObject"
adoConnection.Open "", "", ""

Set RootDSE = GetObject("LDAP://RootDSE")
searchRoot = RootDSE.Get("defaultNamingContext")

'On Error Resume Next
Set adoRecordset = adoConnection.Execute _
("<LDAP://" & searchRoot & ">;(&(objectCategory=Person) (sAMAccountName=" &
strUserName & "));" _
& "Name,ADsPath,sAMAccountName;SubTree")

While Not adoRecordset.EOF
wscript.echo adoRecordset.Fields.Item("Name").Value
adoRecordset.MoveNext
Wend
========================================================== =====



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top