Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check to See if User Exist in AD

Posted on 2006-11-15
5
Medium Priority
?
392 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:dion_p1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 17952806
"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
 

Author Comment

by:dion_p1
ID: 17953061
After: strPassword = Lcase(split(strData, ",")(7))

Before: Next

The User ID is strUserID

Please Edit Posting of Email Address Good Pickup!
0
 

Author Comment

by:dion_p1
ID: 17954243
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 17955574
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question