Solved

Check to See if User Exist in AD

Posted on 2006-11-15
5
385 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

735 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