[AccessD] Setting Up a Doman - Code for Computer Info

Drew Wutka DWUTKA at Marlow.com
Tue Jun 23 11:12:50 CDT 2009


Sorry for the long delay in finishing this subject, been swamped!

So, the last tutorial had us connecting a client to our domain.  And now
it's time to see what you can do with Active Directory programmatically.
That was a big reason of posting this to AccessD, because as developers,
you can use the information in AD (Active Directory) inside your apps.

First up, computer information:  (Please watch for linewrap)

Function ListADComputers()
On Error GoTo ErrorHandler
Dim Comm As ADODB.Command
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strCurrentDomain As String
Dim cnn As ADODB.Connection
Dim strResult As String
Set cnn = New ADODB.Connection
cnn.Provider = "ADsDSOObject"
cnn.Open "Active Directory Provider"
Set Comm = New Command
Comm.ActiveConnection = cnn
Comm.Properties("Page Size") = 100
Comm.Properties("Timeout") = 60
Comm.Properties("Sort On") = "Name"
Comm.Properties("Cache Results") = False
strCurrentDomain =
GetObject("LDAP://RootDSE").Get("defaultNamingContext")
strSQL = "<LDAP://" & strCurrentDomain & _
">;(objectClass=computer);Name,operatingSystem;subtree"
Comm.CommandText = strSQL
Set rs = Comm.Execute
If rs.EOF = False Then
    rs.MoveFirst
    Do Until rs.EOF = True
        If Not IsNull(rs.Fields("Name").Value) Then
            strResult = rs.Fields("Name").Value
            If Not IsNull(rs.Fields("operatingSystem").Value) Then
                strResult = strResult & " - OS: " &
rs.Fields("operatingSystem").Value
            Else
                strResult = strResult & " - OS: UNKNOWN"
            End If
            Debug.Print strResult
        End If
        rs.MoveNext
    Loop
End If
rs.Close
Set rs = Nothing
Set Comm = Nothing
cnn.Close
Set cnn = Nothing

Exit Function

ErrorHandler:

If Err.Number = -2147023541 Then
    Err.Clear
Else
    If Err.Number = 457 Then
        Resume Next
    Else
        MsgBox Err.Number & " - " & Err.Description
    End If
End If
End Function


As you can see, you can use ADO and LDAP to query Active Directory.  In
the query structure, you are telling LDAP that you are querying for the
object class 'computer'.  Here is a link to show you what else is
available for that class:
http://msdn.microsoft.com/en-us/library/ms680987(VS.85).aspx

A little explanation, on the 'derived from' column, that is telling you
where that information is coming from, so 'Address' is coming from a
Person class, so it's not native to a computer class.

How to use this, let's say we want the service packs, the query would
be:

strSQL = "<LDAP://" & strCurrentDomain & _
">;(objectClass=computer);Name,operatingSystem,operatingSystemServicePac
k;subtree"

Note that on the page from the link, it doesn't show
'operatingSystemServicePack', there are spaces or dashes, clicking on
that property will give you the LDAP display name, which is what you
need to use in the query.  

Then, in our sample function, just add this part after the first If Not
Isnull, else, end if: (Watch for line wrap)

If Not IsNull(rs.Fields("operatingSystemServicePack").Value) Then
                strResult = strResult & " - Role: " &
rs.Fields("operatingSystemServicePack").Value
            Else
                strResult = strResult & " - Role: UNKNOWN"
            End If

You can use a list of computers for all sorts of things. For general
customer info, remote installation, etc.

Next up, User Information!

Drew
The information contained in this transmission is intended only for the person or entity 
to which it is addressed and may contain II-VI Proprietary and/or II-VI Business 
Sensitive material. If you are not the intended recipient, please contact the sender 
immediately and destroy the material in its entirety, whether electronic or hard copy. 
You are notified that any review, retransmission, copying, disclosure, dissemination, 
or other use of, or taking of any action in reliance upon this information by persons 
or entities other than the intended recipient is prohibited.





More information about the AccessD mailing list