vbs ldap query issues - vbscript

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

Related

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.

Get SamAccountname using DisplayName in Active Directory

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

VBScript for creating local account and adding to admin group used to work prior to logout / login to test newly created account:

Set objShell = CreateObject("Wscript.Shell")
Set objEnv = objShell.Environment("Process")
strComputer = objEnv("COMPUTERNAME")
strUser = inputbox("Enter the username for the new admin account.")
strPass = inputbox("Enter the password for the new account.")
Set colAccounts = GetObject("WinNT://" & strComputer & ",computer")
Set objUser = colAccounts.Create("user", strUser)
objUser.SetPassword strPass
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
objPasswordExpirationFlag = ADS_UF_DONT_EXPIRE_PASSWD
objUser.Put "userFlags", objPasswordExpirationFlag
objUser.SetInfo
Set Group = GetObject("WinNT://" & strComputer & "/Administrators,group")
Group.Add(objUser.ADspath)
Sigh. A simple matter of "Run as Administrator".

Search for string using VBscript and copy that column to another worksheet

I have Book1.csv and Book2.xlsx. Book1.csv has many columns with data. Each column has unique title in first ROW. I need to find column with title “Processor Time” and copy all available data in this column to column1 in Book2.xlsx using VBscript. Please help.
You can use ADO to get the column:
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"
cn.Open strcon
strSQL = "Select [Processor Time] From [Book1.csv]"
rs.Open strSQL, cn
MsgBox rs.GetString
You can use automation with Excel:
Set xl = CreateObject("Excel.Application")
You can write to Excel from a recordset with:
xl.Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs
You can post back if you have problems putting the bits together.
EDIT re Comment
Try switching your code around a little:
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set objWorkbook1=xl.Workbooks.Open("C:\Docs\book2.xlsx")
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"
cn.Open strcon
strSQL = "Select [Processor Time] From [Book1.csv]"
rs.Open strSQL, cn
objWorkbook1.Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rs

Resources