Solved

Check to See if User Exist in AD

Posted on 2006-11-15
5
380 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now