Check to See if User Exist in AD

Im trying to check to see if a user exist in AD.
The username will be strUserID the OU Will need to be non specific.
Help Appreciated

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Dont Edit Above
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
          strInputFile = "C:\Documents and Settings\Dion Parsons\My Documents\ICT Stuff\Scripts\Auto User Importer\Working\"
      strExportFile = "C:\Documents and Settings\Dion Parsons\My Documents\ICT Stuff\Scripts\Auto User Importer\Working\"

          strEmailOk = "YES"
          strExchangeSrv = "10.x.y.z"
      strEmailFrm = "TechSupport@MyCompany.com"
      strEmailTo = "User@Company.com"
      strEmailAttachment = "C:\Documents and Settings\Dion Parsons\My Documents\ICT Stuff\Scripts\Auto User Importer\Working\"
      strEmailSub = "New User Added to Network "
      strEmailBody = "See Attached for New Users and Password Created on "

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Dont Edit Below
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Dim objRootDSE, objContainer, objUser, objFileSys, strpassword, strExit, strEnter, strEmailOk
Dim strDomainOU, strRtn, strUserID, strFirstname, strSurname, strYear, strGroup, strUserPath, strCACLS, strMKDIR
Dim strInputFile, objInputFile, strInputData, strData, strDomain, strCampus, strHomeFolder, rootDSE, domainObject, domainContainer
Set objFileSys = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Current Date Function
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Function Format(vExpression, sFormat)
        set fmt = CreateObject("MSSTDFMT.StdDataFormat")
        fmt.Format = sFormat
        set rs = CreateObject("ADODB.Recordset")
        rs.Fields.Append "fldExpression", 12 ' adVariant
        rs.Open
        rs.AddNew
        set rs("fldExpression").DataFormat = fmt
        rs("fldExpression").Value = vExpression
        Format = rs("fldExpression").Value
        rs.close: Set rs = Nothing: Set fmt = Nothing
End Function
strCDate = format(now(),"dd-mm-yyyy")
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Read Original Export and Add Random Passwords
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Set oInputFile = objFileSys.OpenTextFile(strInputFile & strcDate & " ExportFromCases.csv")
strInputData = Split(oInputFile.ReadAll, vbNewline)

On Error Resume Next
For each strData In strInputData

Dim intPassword
Randomize
intPassword = Int(1234567 * Rnd() + 1)
      Set file = objFileSys.CreateTextFile(strExportFile & strCDate & " ExportWithPasswords.csv",TRUE)
      file.writeline(strData & "," & intPassword)
Next
file.close
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Read ExportWithPasswords
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Set oInputFile = objFileSys.OpenTextFile(strExportFile)
strInputData = Split(oInputFile.ReadAll, vbNewline)
On Error Resume Next
For each strData In strInputData
          strUserID= Ucase(split(strData, ",") (0))
          strFirstname = Lcase(split(strData, ",") (1))
          strFirstname = Ucase(left(strFirstname, 1)) & mid(strFirstname, 2, len(strFirstname))
          strSurname = Lcase(split(strData, ",") (2))
          strSurname = Ucase(left(strSurname, 1)) & mid(strSurname, 2, len(strSurname))
          strSurname = Replace(strSurname, "'", "")
          strGroup = Ucase(split(strData, ",") (3))
          strYear = Lcase (Split(strData, ",") (4))
          strHomeFolder = Lcase(split(strData, ",")(4))
      strEnter = LCase(Split(strData, ",")(5))
      strExit = LCase (Split(strData, ",")(6))
      strPassword = Lcase(split(strData, ",")(7))

Next
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Email If strEmailOk = Yes
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strEmailOk = "YES" then
Call EmailUser
Else
End If

Sub EmailUser
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing", _
cdoSendUsingPort = 2, _
cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
'Create the CDO connections.
Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
'SMTP server configuration.
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort
'Set the SMTP server address here.
.Item(cdoSMTPServer) = strExchangeSrv
.Update
End With
'Set the message properties.
With iMsg
Set .Configuration = iConf
.To = strEmailTo
.From = strEmailFrm
.Subject = strEmailSub & strCDate
.TextBody = strEmailBody & strCDate
End With
'An attachment can be included.
iMsg.AddAttachment strEmailAttachment & strCDate & " ExportWithPasswords.csv"
'Send the message.
iMsg.Send ' send the message.
End Sub

wscript.close
dion_p1Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sirbountyConnect With a Mentor Commented:
My mistake - I think I was confused (recovering from an illness :\)
Just a slight correction to the above (with some 'renaming' for clarification).
We just return the results of the rs query - if it's empty, the user isn't there...


Place this in the position that you indicated...

blnExists=DoesExist(strUserID)  'Presumably strUserID refers to the logon name / SAM Account Name?

Then add this function at some other location (at the bottom maybe)

Function DoesExist(byVal usr)
  DoesExist=False  'Return False by default
  Set cn = CreateObject("ADODB.Connection")
  Set cmd = CreateObject("ADODB.Command")
  cn.Provider = "ADsDSOObject"
  cn.Open "Active Directory Provider"
  Set cmd.ActiveConnection = cn

  'The next two lines are in order to try and determine your domain.  You can bypass this by simply replacing these lines with strLDAP="DC=YourDomain.Com"
  Set objShell = CreateObject("WScript.Shell")
  strLDAP="DC=" & Replace(objShell.ExpandEnvironmentStrings("%UserDNSDomain%"), ".",",DC=")

  cmd.CommandText = "SELECT DistinguishedName FROM 'LDAP://" & strLDAP & "' WHERE saMAccountName='" & usr & "'"
  Set objRS=cmd.Execute
  If Not objRS.Eof Then DoesExist=True
End Function
0
 
sirbountyCommented:
"Im trying to check to see if a user exist in AD. "

Shouldn't be a problem...where do you want the processing to occur?

Also - would you like the specific email addresses to be editted from this posting?  I doubt they're necessary for the question and can only attrach SpamBots...
0
 
dion_p1Author Commented:
After: strPassword = Lcase(split(strData, ",")(7))

Before: Next

The User ID is strUserID

Please Edit Posting of Email Address Good Pickup!
0
 
dion_p1Author Commented:
Problem is that some account in AD might be disabled, so i dont want to confuse the result.
Idea is to Check if strUserID exist in Domain if it does then do nothing, if it Doesn't then Create it. I have the Things i need i think to create the user but not find it if it exist.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.