Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

What do I do with this code now?

stpn2mestpn2me Posts: 5Member
I have an example of code that requests information about a specific user from active directory. I just dont know where to put my specific network information in for the example info. Can anyone help me? Where in this code do I need to substitute my info? Do I replace loginname below with the name I plan to search for?

Public Function UserInfo(LoginName As String) As String
'PURPOSE: Display information that is available in 'the Active Directory about a given user

'PARAMETER: Login Name for user

'RETURNS: String with selected information about 'user, or empty string if there is no such 'login on the current domain

'REQUIRES: Windows 2000 ADSI, LDAP Provider 'Proper Security Credentials.

'EXAMPLE: msgbox UserInfo("Administrator")

Dim conn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim oRoot As IADs
Dim oDomain As IADs
Dim sBase As String
Dim sFilter As String
Dim sDomain As String

Dim sAttribs As String
Dim sDepth As String
Dim sQuery As String
Dim sAns As String

Dim user As IADsUser

On Error GoTo ErrHandler:

'Get user Using LDAP/ADO. There is an easier way 'to bind to a user object using the WinNT provider, 'but this way is a better for educational purposes Set oRoot = GetObject("LDAP://rootDSE") 'work in the default domain sDomain = oRoot.Get("defaultNamingContext") Set oDomain = GetObject("LDAP://" & sDomain) sBase = "<" & oDomain.ADsPath & ">"
'Only get user name requested
sFilter = "(&(objectCategory=person)(objectClass=user)(name=" _
& LoginName & "))"
sAttribs = "adsPath"
sDepth = "subTree"

sQuery = sBase & ";" & sFilter & ";" & sAttribs & ";" & sDepth

conn.Open _
"Data Source=Active Directory Provider;Provider=ADsDSOObject"

Set rs = conn.Execute(sQuery)

If Not rs.EOF Then
Set user = GetObject(rs("adsPath"))
With user

'if the attribute is not stored in AD,
'an error will occur. Therefore, this
'will return data only from populated attributes
On Error Resume Next

sAns = "First Name: " & .FirstName & vbCrLf
sAns = sAns & "Last Name " & .LastName & vbCrLf
sAns = sAns & "Employee ID: " & .EmployeeID & vbCrLf
sAns = sAns & "Title: " & .Title & vbCrLf
sAns = sAns & "Division: " & .Division & vbCrLf
sAns = sAns & "Department: " & .Department & vbCrLf
sAns = sAns & "Manager: " & .Manager & vbCrLf

sAns = sAns & "Phone Number: " & .TelephoneNumber & vbCrLf
sAns = sAns & "Fax Number: " & .FaxNumber & vbCrLf

sAns = sAns & "Email Address: " & .EmailAddress & vbCrLf
sAns = sAns & "Web Page: " & .HomePage & vbCrLf
sAns = sAns & "Last Login: " & .LastLogin & vbCrLf
sAns = sAns & "Last Logoff: " & .LastLogoff & vbCrLf

sAns = sAns & "Account Expiration Date: " _
& .AccountExpirationDate & vbCrLf

'IN RC2, this returned 1/1/1970 when password
'never expires option is set
sAns = sAns & "Password Expiration Date: " _
& .PasswordExpirationDate

End With
End If
UserInfo = sAns
ErrHandler:

On Error Resume Next
If Not rs Is Nothing Then
If rs.State <> 0 Then rs.Close
Set rs = Nothing
End If

If Not conn Is Nothing Then
If conn.State <> 0 Then conn.Close
Set conn = Nothing
End If

Set oRoot = Nothing
Set oDomain = Nothing
End Function

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : I have an example of code that requests information about a specific user from active directory. I just dont know where to put my specific network information in for the example info. Can anyone help me? Where in this code do I need to substitute my info? Do I replace loginname below with the name I plan to search for?
    :

    Not really sure what the rest of the code is, but at the point where you need the user information:
    [code]
    ...
    Dim sInfo As String

    sInfo = UserInfo([italic]NameOfUser[/italic])
    ...
    [/code]

    I suggest you put the UserInfo function code in a module.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry

  • stpn2mestpn2me Posts: 5Member
    : : I have an example of code that requests information about a specific user from active directory. I just dont know where to put my specific network information in for the example info. Can anyone help me? Where in this code do I need to substitute my info? Do I replace loginname below with the name I plan to search for?
    : :
    :
    : Not really sure what the rest of the code is, but at the point where you need the user information:
    : [code]
    : ...
    : Dim sInfo As String
    :
    : sInfo = UserInfo([italic]NameOfUser[/italic])
    : ...
    : [/code]
    :
    : I suggest you put the UserInfo function code in a module.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    :
    :

    Hey thanks. I am still no closer, but I will try your solution...thanks....
  • stpn2mestpn2me Posts: 5Member
    : : : I have an example of code that requests information about a specific user from active directory. I just dont know where to put my specific network information in for the example info. Can anyone help me? Where in this code do I need to substitute my info? Do I replace loginname below with the name I plan to search for?
    : : :
    : :
    : : Not really sure what the rest of the code is, but at the point where you need the user information:
    : : [code]
    : : ...
    : : Dim sInfo As String
    : :
    : : sInfo = UserInfo([italic]NameOfUser[/italic])
    : : ...
    : : [/code]
    : :
    : : I suggest you put the UserInfo function code in a module.
    : :
    : : Best Regards,
    : : Richard
    : :
    : : The way I see it... Well, it's all pretty blurry
    : :
    : :
    :
    : Hey thanks. I am still no closer, but I will try your solution...thanks....
    :
    By the way, is that the only place I need to change information that you can see?

    Stpn2me
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    As far as I can see, someone gave you a function you could use in your code.
    So basically, you just add that function to a module unchanged.
    Then when you need it, you let the function work it's magic by calling it with the right username.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry

  • stpn2mestpn2me Posts: 5Member
    : As far as I can see, someone gave you a function you could use in your code.
    : So basically, you just add that function to a module unchanged.
    : Then when you need it, you let the function work it's magic by calling it with the right username.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    :
    :
    Do you think I can run the code in standalone only putting in my specific domain information? I want to use this code to extract specific information from AD that the GUI wont. Got any ideas?
Sign In or Register to comment.