Get SamAccountname using DisplayName in Active Directory - vbscript

I need vbscript that will do an LDAP query using a displayname, and retrieve the SamAccountName for me.

Something like this should do it:
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
strDomainName = "dc=YOURDOMAIN,dc=com"
strUserCN = "FIRSTNAME LASTNAME"
objCommand.CommandText = "<LDAP://" & strDomainName & ">;(&(objectCategory=person)(objectClass=user)(cn=" & strUserCN & "));samAccountName;subtree"
Set objRecordSet = objCommand.Execute
If Not objRecordset.EOF Then
WScript.Echo objRecordSet.Fields("samAccountName")
End If
objConnection.Close
Set objRecordSet = Nothing
Set objConnection = Nothing
Set objCommand = Nothing

Great script ( and thankyou ) but at the moment only shows the first result in the recordset - the displaying the output bit needs to something more like this:
DO until objRecordset.EOF = TRUE
WScript.Echo objRecordSet.Fields("samAccountName")
objRecordSet.MoveNext
Loop

Related

How to return a XMLDom object forom classic asp function [duplicate]

I am loosing my hair on VBScript. How the heck can I pass a reference as return value of a function?
Currently my code looks like this:
Set objUser = FindUser("bendert")
REM Searches Directory for the User
Function FindUser(UserLoginName)
Wscript.Echo "Querying AD to retrieve user-data"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
'Get user Using LDAP/ADO. There is an easier way
'to bind to a user object using the WinNT provider,
'but this way is a better for educational purposes
Set oRoot = GetObject("LDAP://rootDSE")
'work in the default domain
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
'Only get data for login name requested
sFilter = "(&(sAMAccountName="& UserLoginName &")(objectClass=user))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
WScript.Echo "LDAP Query is:" & sQuery &""
objCommand.CommandText=sQuery
Set objRecordSet = objCommand.Execute
FindUser = GetObject(objRecordSet.Fields("adspath"))
WScript.Echo "You E-Mail Address is: " & objUser.EmailAddress
objConnection.Close
End Function
Unfortunatley VBScript throws an error on the line where I make an assignment to the function's return value.
FindUser = GetObject(objRecordSet.Fields("adspath"))
The Error looks like "wrong number of arguments or invalid property assignment".
What am I doing wrong?
Looks like you need:
Set FindUser = GetObject(objRecordSet.Fields("adspath"))
Set FindUser = ...
http://msdn.microsoft.com/en-us/library/4afksd44%28VS.85%29.aspx

Put DN into variable VBS

I am creating a script that will allow me to enter a username in our domain, and have it look up attributes from their AD profile.
So first I am getting the users' DN. Once I have that - I can run;
Set objAD = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & ***I NEED DN HERE***)
and query specific attributes to be output.
So what I need to do is somehow get the DN into a variable to put into the LDAP query. I know (I think) I need to get it from the Do Loop below, but am having a complete blank and can't figure out how to just put the whole DN into a variable.
Username = InputBox("Enter UserName to lookup...")
Set rootDSE = GetObject("LDAP://RootDSE")
base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"
fltr = "(&(objectClass=user)(objectCategory=Person)" & "(sAMAccountName=" & UserName & "))"
attr = "distinguishedName,sAMAccountName"
scope = "subtree"
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADsDSOObject"
conn.Open "Active Directory Provider"
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = base & ";" & fltr & ";" & attr & ";" & scope
Set rs = cmd.Execute
Do Until rs.EOF
WScript.Echo rs.Fields("distinguishedName").Value
rs.MoveNext
Loop
rs.Close
conn.Close
In case anyone has the same problem - it was an easy fix.
Just needed to write it to a variable instead of echoing.
Do Until rs.EOF
strDN = rs.Fields("distinguishedname").value
rs.MoveNext
Loop

sAMAccountName and case-sensitivity

I'm able to fetch the user details for a particular ADS user through the following vbs code.
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://"& objDomain.Get("distinguishedName") &">;" & _
"(&(objectclass=user)(objectcategory=person)(sAMAccountName=" & strUserName & "));" & _
"cn,displayName;subtree"
objCommand.Execute
Here could I want to apply LCase to sAMAccountName before comparing with strUserName. Is this possible?
sAMAccountName is case-insensitive, so it doesn't matter if the value of strUserName is in upper, lower, or mixed case.

vbs ldap query issues

i am currently trying to use my small knowledge of scripting to search through ldap find a user based on a variable then get my the displayname for that user.
so far i ahve the below and im stuck
On Error Resume Next
Dim objNetwork
Dim userName
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.UserName
WScript.Echo userName
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.CommandText = _
"<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.Fields("Name").Value = username
objRecordSet.MoveNext
Loop
Wscript.Echo objRecordSet.Fields("Name").Value
= PLEASE HELP!!
I scartached the above and am now trying this but get syntax errors
option explicit dim cmd, cn, rs, objRoot,
set cmd = createobject("ADODB.Command")
set cn = createobject("ADODB.Connection")
set rs = createobject("ADODB.Recordset")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn set
objRoot = getobject("LDAP://RootDSE") cmd.commandtext = "<LDAP://dn=domain,dn=com">;(&(objectClass=user)(!(objectClass=computer) (sAMAccountName=first.last))));name,displayname;subtree"
cmd.properties("page size")=1000 set rs = cmd.execute
wscript.echo rs("name")
I doubt the attribute name is Name, and try displayName instead in this line:
"<LDAP://dc=domain,dc=com>;(objectCategory=User);Name;Subtree"
ended up getting with this
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
strDomainName = "dc=domain,dc=com"
strUserCN = username
objCommand.CommandText = "<LDAP://" & strDomainName & ">;(&(objectCategory=person) (objectClass=user)(cn=" & strUserCN & "));displayName;subtree"
Set objRecordSet = objCommand.Execute
If Not objRecordset.EOF Then
strray = split(objRecordSet.Fields("displayName"),"(")
strdat = strray(0)
username = trim(strdat)
End If

Returning References from Function in VBScript

I am loosing my hair on VBScript. How the heck can I pass a reference as return value of a function?
Currently my code looks like this:
Set objUser = FindUser("bendert")
REM Searches Directory for the User
Function FindUser(UserLoginName)
Wscript.Echo "Querying AD to retrieve user-data"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
'Get user Using LDAP/ADO. There is an easier way
'to bind to a user object using the WinNT provider,
'but this way is a better for educational purposes
Set oRoot = GetObject("LDAP://rootDSE")
'work in the default domain
sDomain = oRoot.Get("defaultNamingContext")
Set oDomain = GetObject("LDAP://" & sDomain)
sBase = "<" & oDomain.ADsPath & ">"
'Only get data for login name requested
sFilter = "(&(sAMAccountName="& UserLoginName &")(objectClass=user))"
sAttribs = "adsPath"
sDepth = "subTree"
sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth
WScript.Echo "LDAP Query is:" & sQuery &""
objCommand.CommandText=sQuery
Set objRecordSet = objCommand.Execute
FindUser = GetObject(objRecordSet.Fields("adspath"))
WScript.Echo "You E-Mail Address is: " & objUser.EmailAddress
objConnection.Close
End Function
Unfortunatley VBScript throws an error on the line where I make an assignment to the function's return value.
FindUser = GetObject(objRecordSet.Fields("adspath"))
The Error looks like "wrong number of arguments or invalid property assignment".
What am I doing wrong?
Looks like you need:
Set FindUser = GetObject(objRecordSet.Fields("adspath"))
Set FindUser = ...
http://msdn.microsoft.com/en-us/library/4afksd44%28VS.85%29.aspx

Resources