Getting data out of Active Directory

  • Hello all,

    I'm working on a project to create a web-based information directory of company employees.  To avoid maintaining a separate list of employees, I would like to pull this list from Active Directory.  I have considered pulling the data into SQL Server periodically (every other night, for example) so I can allow the data to be indexed and easily searchable.

    Do any of you have any experience doing this kind of thing?  I would like to hear about your experiences, if so.

    Thanks

    Tim

  • Investigate ADSI.  Pretty straight-forward, once you get it down...just like writing a query.  Thanks.

    Chris

  • Hopefully this will help. 

    Here's a sample I found on the net some time back:

    ' Get User Information gui.vbs

    ' Revision history

     

    ' Created 11-26-2001 by Ralph Montgomery - Firsthealth of the Carolinas (rmonty@myself.com)

    ' Initialize the variable farm

    Dim objUserName, objUserDomain, oGroup, objUser, gList, WshShell, sMessage, sTitle

    Dim objDomain, vDomain, vUserName

    Dim objChangePwdTrue, objChangePwd, objUserProfile

    Dim objPwdExpiresTrue, objFlags

    Dim objAcctDisabled, intPwdExpired, objPwdExpiredTrue

     ' Set WshShell and WshFSO

     Set WshFSO = WScript.CreateObject("Scripting.FileSystemObject")

     Set WshShell = WScript.CreateObject("WScript.Shell")

     ' Pull Environment variables for domain/user

     vdomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")

     vUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")

     SysTest ' sub routine to check for Script Version/ADSI installed

     GetInfo ' sub routing to get user information

     on error resume Next

     ' Define user then retrieve and store groups in a list

     For Each oGroup In objUser.Groups

      If gList <> "" Then

       gList = gList & ", "

      End If

      If (Len(gList) > Instr(gList, vbCrLf) + 50) Then

       gList = gList & vbCrLf

      End If

      gList = gList & oGroup.Name

     Next

     'check for expired password

     intPwdExpired = objUser.Get("PasswordExpired")

     If intPwdExpired = 1 Then

      objPwdExpiredTrue = "Yes"

      Else objPwdExpiredTrue = "No"

     End If

     

     'Check for Must Change Password Flag

     objFlags = objUser.Get("UserFlags")

     If (objFlags And &H00040) <> 0 Then

      objChangePwdTrue = "No"

      Else objChangePwdTrue = "Yes"

     End If

     ' Is password set to NEVER expire?

     objPwdExpires = objUser.Get("UserFlags")

     If (objPwdExpires And &H10000) <> 0 Then

      objPwdExpiresTrue = "Yes"

      Else objPwdExpiresTrue = "No"

     End If

     

     ' Is the account disabled?

     If objUser.AccountDisabled = True Then

      objAccountDisabled = "Yes"

      Else objAccountDisabled = "No"

     End If

     

     'How many wrong logins?

     objBadLogins = objUser.BadLoginCount

     

     ' How old is the current password?

     oPwdAge = objUser.Get("PasswordAge")

     objPwdAge = FormatNumber(((oPwdAge/60)/60)/24, 0)

     ' Set Profile path to tabs if blank

     If objUserProfile = "" Then

      objUserProfile= "" & vbCrLf

      Else objUserProfile = objUserProfile

     End If

     ' Set sMessage box variables to null 

     sMessage =""

     'popup user information: each line broken up for better reading

     sMessage = sMessage & "Domain: " & objDomain & vbCrLf & "User Name: " & objUserName & _

      vbCrLf & "Full Name: " & objUser.FullName & vbCrLf & vbCrLf & "Description: " & _

      objUser.Description & vbCrLf & vbCrLf

     sMessage = sMessage & "Password Expired: " & objPwdExpiredTrue & vbCrLf & _

      "User can change Pwd: " & objChangePwdTrue &  vbCrLf & "Account Disabled: " & _

      objAccountDisabled & vbCrLf & vbCrLf & "Account Locked Out: " & objUser.IsAccountLocked & _

      vbCrLf & "Password Age: " & objPwdAge & vbCrLf & vbCrLf

     sMessage = sMessage &  "Bad Logins: " & objBadLogins & vbCrLf & vbCrLf & "Last logon: " & _

      objUser.LastLogin & vbCrLf & "Password Never Expires: " & objPwdExpiresTrue & _

      vbCrLf & "Password Minimum Length: " & objUser.PasswordMinimumLength & vbCrLf & vbCrLf

     sMessage = sMessage & "User Profile Path: " & objUserProfile & "Home Directory: " & _

      objUser.HomeDirectory & vbCrLf & "Login Script: " & objUser.LoginScript & vbCrLf & vbCrLf

     sMessage = sMessage & "User Groups: " & vbCrLf & vbCrLf & gList & vbCrLf

     ' Display User Information!

     WshShell.Popup sMessage,0,"User Information for: " & objUserName & " in " & objDomain

     Set f = WshFso.OpenTextFile(objUserName & ".txt", 8, True)

     f.WriteLine sMessage

     f.Close

    '*******************************************************************************************

    Sub GetInfo()

     ' Retrieve Domain from user

     sMessage = "Please enter the domain to search." & vbCrLf & vbCrLf & _

      "Default is: " & vDomain & vbCrLf & vbCrLf

     sMessage = sMessage & "Hit Cancel or enter a blank to quit"

     sTitle = "Domain to Search"

     'get resource domain name, domain default

     objDomain = InputBox(sMessage, sTitle, vDomain)

     

     ' Evaluate the user input.

     If objDomain = "" Then    ' Cancelled by the user

         WScript.quit

     End If

     

     ' Set sMessage box variables to null 

     ssMessage = ""

     ssTitle = ""

     on error resume Next

     ' Define username dialog box variables.

     sMessage = "Please enter the USER Login ID" & vbCrLf & vbCrLf & _

      "Default is: " & vUserName & vbCrLf & vbCrLf

     sMessage = sMessage & "Hit Cancel or enter a blank to quit"

     sTitle = "USER Login ID"

     'get resource domain name, domain default via input box

     objUserName = InputBox(sMessage, sTitle, vUserName)

     ' Evaluate the user input.

     If objUserName = "" Then    ' Cancelled by the user

         WScript.quit

     End If

     

     ' Display Just a minute!

     sMessage = "This may take a few seconds. . ."

     WshShell.Popup sMessage,2,"One moment please. . . "

     sMessage = ""

     

     'Attempt to bind to the user

     Set objUser = GetObject("WinNT://"& objDomain &"/"& objUserName & "",user)

     If Err Then

      msgNoUser =  "Error: Could not bind to the following user: " & vbCrLf _

       & vbCrLf & "WinNT://" & objDomain &"/"& objUserName & vbCrLf & vbCrLf _

       & "Please verify your domain and user name and try again"

      WshShell.Popup msgNoUser,0,"Error retrieving information",vbCritical

      GetInfo

     Else

      msgConnected = "Connected to user WinNT://" & objDomain &"/"& objUserName & vbCrLf

      WshShell.Popup msgConnected,0,"Connected To " & objUserName & " in " & objDomain,    vbInformation

     

     End If

    End Sub

    Sub SysTest()

    on error resume Next

    ' WSH version tested

    Major = (ScriptEngineMinorVersion())

    Minor = (ScriptEngineMinorVersion())/10

    Ver = major + minor

    'Need version 5.5

     If err.number or ver = 5.6 then

     message = "You must load Version 5.5 (or later) of Windows Script Host" & vbCrLf &_

       vbCrLf & "Located at: \\servername\share\scr56en.exe"

       WScript.Quit

    End If

    'Test for ADSI

    err.clear

    key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\version"

    key2 = WshShell.RegRead (key)

    if Err <> 0 then

       message = message & "ADSI 5.2  must be installed on local workstation to continue" & vbCrLf &_

       vbCrLf & "Located at: \\servername\share\adsi5.2.exe"

     WshShell.Popup message,0,"Workstation Setup Error",vbCritical

     WScript.Quit

    End If

    'Test whether the host is CScript.exe.

    'G. Born code...

        If IsBatch = "TRUE" Then

     If (Not IsCScript()) Then

      message = "You must set default host to cscript to run as a batch." &vbcrlf &_

       "Use the command wscript //h:cscript" 'popup closes to avoid desktop hell

      WshShell.Popup message,3,"Workstation Setup Error",vbCritical

      WScript.Quit            ' Terminate script.

     End If

        End if

    End Sub

    Function IsCScript()

        ' Check whether CScript.exe is the host.

        If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then

            IsCScript = True

        Else

            IsCScript = False

        End If

    End Function

     

    '**** Script Ends

     

    So long, and thanks for all the fish,

    Russell Shilling, MCDBA, MCSA 2K3, MCSE 2K3

  • Hopefully this will help. 

    Here's a sample I found on the net some time back:

    ' Get User Information gui.vbs

    ' Revision history

     

    ' Created 11-26-2001 by Ralph Montgomery - Firsthealth of the Carolinas (rmonty@myself.com)

    ' Initialize the variable farm

    Dim objUserName, objUserDomain, oGroup, objUser, gList, WshShell, sMessage, sTitle

    Dim objDomain, vDomain, vUserName

    Dim objChangePwdTrue, objChangePwd, objUserProfile

    Dim objPwdExpiresTrue, objFlags

    Dim objAcctDisabled, intPwdExpired, objPwdExpiredTrue

     ' Set WshShell and WshFSO

     Set WshFSO = WScript.CreateObject("Scripting.FileSystemObject")

     Set WshShell = WScript.CreateObject("WScript.Shell")

     ' Pull Environment variables for domain/user

     vdomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")

     vUserName = WshShell.ExpandEnvironmentStrings("%USERNAME%")

     SysTest ' sub routine to check for Script Version/ADSI installed

     GetInfo ' sub routing to get user information

     on error resume Next

     ' Define user then retrieve and store groups in a list

     For Each oGroup In objUser.Groups

      If gList <> "" Then

       gList = gList & ", "

      End If

      If (Len(gList) > Instr(gList, vbCrLf) + 50) Then

       gList = gList & vbCrLf

      End If

      gList = gList & oGroup.Name

     Next

     'check for expired password

     intPwdExpired = objUser.Get("PasswordExpired")

     If intPwdExpired = 1 Then

      objPwdExpiredTrue = "Yes"

      Else objPwdExpiredTrue = "No"

     End If

     

     'Check for Must Change Password Flag

     objFlags = objUser.Get("UserFlags")

     If (objFlags And &H00040) <> 0 Then

      objChangePwdTrue = "No"

      Else objChangePwdTrue = "Yes"

     End If

     ' Is password set to NEVER expire?

     objPwdExpires = objUser.Get("UserFlags")

     If (objPwdExpires And &H10000) <> 0 Then

      objPwdExpiresTrue = "Yes"

      Else objPwdExpiresTrue = "No"

     End If

     

     ' Is the account disabled?

     If objUser.AccountDisabled = True Then

      objAccountDisabled = "Yes"

      Else objAccountDisabled = "No"

     End If

     

     'How many wrong logins?

     objBadLogins = objUser.BadLoginCount

     

     ' How old is the current password?

     oPwdAge = objUser.Get("PasswordAge")

     objPwdAge = FormatNumber(((oPwdAge/60)/60)/24, 0)

     ' Set Profile path to tabs if blank

     If objUserProfile = "" Then

      objUserProfile= "" & vbCrLf

      Else objUserProfile = objUserProfile

     End If

     ' Set sMessage box variables to null 

     sMessage =""

     'popup user information: each line broken up for better reading

     sMessage = sMessage & "Domain: " & objDomain & vbCrLf & "User Name: " & objUserName & _

      vbCrLf & "Full Name: " & objUser.FullName & vbCrLf & vbCrLf & "Description: " & _

      objUser.Description & vbCrLf & vbCrLf

     sMessage = sMessage & "Password Expired: " & objPwdExpiredTrue & vbCrLf & _

      "User can change Pwd: " & objChangePwdTrue &  vbCrLf & "Account Disabled: " & _

      objAccountDisabled & vbCrLf & vbCrLf & "Account Locked Out: " & objUser.IsAccountLocked & _

      vbCrLf & "Password Age: " & objPwdAge & vbCrLf & vbCrLf

     sMessage = sMessage &  "Bad Logins: " & objBadLogins & vbCrLf & vbCrLf & "Last logon: " & _

      objUser.LastLogin & vbCrLf & "Password Never Expires: " & objPwdExpiresTrue & _

      vbCrLf & "Password Minimum Length: " & objUser.PasswordMinimumLength & vbCrLf & vbCrLf

     sMessage = sMessage & "User Profile Path: " & objUserProfile & "Home Directory: " & _

      objUser.HomeDirectory & vbCrLf & "Login Script: " & objUser.LoginScript & vbCrLf & vbCrLf

     sMessage = sMessage & "User Groups: " & vbCrLf & vbCrLf & gList & vbCrLf

     ' Display User Information!

     WshShell.Popup sMessage,0,"User Information for: " & objUserName & " in " & objDomain

     Set f = WshFso.OpenTextFile(objUserName & ".txt", 8, True)

     f.WriteLine sMessage

     f.Close

    '*******************************************************************************************

    Sub GetInfo()

     ' Retrieve Domain from user

     sMessage = "Please enter the domain to search." & vbCrLf & vbCrLf & _

      "Default is: " & vDomain & vbCrLf & vbCrLf

     sMessage = sMessage & "Hit Cancel or enter a blank to quit"

     sTitle = "Domain to Search"

     'get resource domain name, domain default

     objDomain = InputBox(sMessage, sTitle, vDomain)

     

     ' Evaluate the user input.

     If objDomain = "" Then    ' Cancelled by the user

         WScript.quit

     End If

     

     ' Set sMessage box variables to null 

     ssMessage = ""

     ssTitle = ""

     on error resume Next

     ' Define username dialog box variables.

     sMessage = "Please enter the USER Login ID" & vbCrLf & vbCrLf & _

      "Default is: " & vUserName & vbCrLf & vbCrLf

     sMessage = sMessage & "Hit Cancel or enter a blank to quit"

     sTitle = "USER Login ID"

     'get resource domain name, domain default via input box

     objUserName = InputBox(sMessage, sTitle, vUserName)

     ' Evaluate the user input.

     If objUserName = "" Then    ' Cancelled by the user

         WScript.quit

     End If

     

     ' Display Just a minute!

     sMessage = "This may take a few seconds. . ."

     WshShell.Popup sMessage,2,"One moment please. . . "

     sMessage = ""

     

     'Attempt to bind to the user

     Set objUser = GetObject("WinNT://"& objDomain &"/"& objUserName & "",user)

     If Err Then

      msgNoUser =  "Error: Could not bind to the following user: " & vbCrLf _

       & vbCrLf & "WinNT://" & objDomain &"/"& objUserName & vbCrLf & vbCrLf _

       & "Please verify your domain and user name and try again"

      WshShell.Popup msgNoUser,0,"Error retrieving information",vbCritical

      GetInfo

     Else

      msgConnected = "Connected to user WinNT://" & objDomain &"/"& objUserName & vbCrLf

      WshShell.Popup msgConnected,0,"Connected To " & objUserName & " in " & objDomain,    vbInformation

     

     End If

    End Sub

    Sub SysTest()

    on error resume Next

    ' WSH version tested

    Major = (ScriptEngineMinorVersion())

    Minor = (ScriptEngineMinorVersion())/10

    Ver = major + minor

    'Need version 5.5

     If err.number or ver = 5.6 then

     message = "You must load Version 5.5 (or later) of Windows Script Host" & vbCrLf &_

       vbCrLf & "Located at: \\servername\share\scr56en.exe"

       WScript.Quit

    End If

    'Test for ADSI

    err.clear

    key = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\version"

    key2 = WshShell.RegRead (key)

    if Err <> 0 then

       message = message & "ADSI 5.2  must be installed on local workstation to continue" & vbCrLf &_

       vbCrLf & "Located at: \\servername\share\adsi5.2.exe"

     WshShell.Popup message,0,"Workstation Setup Error",vbCritical

     WScript.Quit

    End If

    'Test whether the host is CScript.exe.

    'G. Born code...

        If IsBatch = "TRUE" Then

     If (Not IsCScript()) Then

      message = "You must set default host to cscript to run as a batch." &vbcrlf &_

       "Use the command wscript //h:cscript" 'popup closes to avoid desktop hell

      WshShell.Popup message,3,"Workstation Setup Error",vbCritical

      WScript.Quit            ' Terminate script.

     End If

        End if

    End Sub

    Function IsCScript()

        ' Check whether CScript.exe is the host.

        If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then

            IsCScript = True

        Else

            IsCScript = False

        End If

    End Function

     

    '**** Script Ends

     

    So long, and thanks for all the fish,

    Russell Shilling, MCDBA, MCSA 2K3, MCSE 2K3

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply