• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

Match AD account display name to PC name ??

Anyone know of a way to map an AD user account Display Name field to the name of the last machine that user logged into?

What Id like to do is take all 44 users in my COT\Accounting security group in AD, and get their computer names.  SMS 2003 can show the username of last logged on user to a computer, but only shows the AD username.  

Even better would be to select the AD security group and the a script/utility can run on its membership, then output a list of last used computer.

Anyone have an idea if this is possible?
1 Solution
Hi, perhaps the easiest way would be to run a login script, which would update each users "Description", or maybe "Notes" field (in the Telephones tab) with the name of the computer they have just logged onto.  Then you can run a script at any time to query that user and see what the field holds.

This would be your login script.

Set objADSysInfo = CreateObject("ADSystemInfo")
strComputer = Mid(Split(objADSysInfo.Computername, ",")(0), 4)
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
objUser.Info = strComputer
'objUser.Description = strComputer
Set objUser = Nothing

And then you could use the below script to search the AD by samAccountName (the login name) and check that value.


strUser = InputBox("Enter a username to see which computer they last used:", "Username", "Enter login name")
If strUser <> "" Then
	strADSPath = Get_LDAP_User_Properties("user", "samAccountName", strUser, "adsPath")
	If Left(strADSPath, 7) = "LDAP://" Then
		Set objUser = GetObject(strADSPath)
		MsgBox "This user last logged onto " & objUser.Info
		Set objUser = Nothing
		MsgBox "Unable to find adsPath for " & strUser
	End If
End If
Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
      ' This is a custom function that connects to the Active Directory, and returns the specific
      ' Active Directory attribute value, of a specific Object.
      ' strObjectType: usually "User" or "Computer"
      ' strSearchField: the field by which to seach the AD by. This acts like an SQL Query's WHERE clause.
      '				It filters the results by the value of strObjectToGet
      ' strObjectToGet: the value by which the results are filtered by, according the strSearchField.
      '				For example, if you are searching based on the user account name, strSearchField
      '				would be "samAccountName", and strObjectToGet would be that speicific account name,
      '				such as "jsmith".  This equates to "WHERE 'samAccountName' = 'jsmith'"
      '	strCommaDelimProps: the field from the object to actually return.  For example, if you wanted
      '				the home folder path, as defined by the AD, for a specific user, this would be
      '				"homeDirectory".  If you want to return the ADsPath so that you can bind to that
      '				user and get your own parameters from them, then use "ADsPath" as a return string,
      '				then bind to the user: Set objUser = GetObject("LDAP://" & strReturnADsPath)
      ' Now we're checking if the user account passed may have a domain already specified,
      ' in which case we connect to that domain in AD, instead of the default one.
      If InStr(strObjectToGet, "\") > 0 Then
            arrGroupBits = Split(strObjectToGet, "\")
            strDC = arrGroupBits(0)
            strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
            strObjectToGet = arrGroupBits(1)
      ' Otherwise we just connect to the default domain
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("defaultNamingContext")
      End If
      strBase = "<LDAP://" & strDNSDomain & ">"
      ' Setup ADO objects.
      Set adoCommand = CreateObject("ADODB.Command")
      Set adoConnection = CreateObject("ADODB.Connection")
      adoConnection.Provider = "ADsDSOObject"
      adoConnection.Open "Active Directory Provider"
      adoCommand.ActiveConnection = adoConnection
      ' Filter on user objects.
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"
      ' Comma delimited list of attribute values to retrieve.
      strAttributes = strCommaDelimProps
      arrProperties = Split(strCommaDelimProps, ",")
      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      ' Define the maximum records to return
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False
      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      ' Enumerate the resulting recordset.
      strReturnVal = ""
      Do Until adoRecordset.EOF
          ' Retrieve values and display.    
          For intCount = LBound(arrProperties) To UBound(arrProperties)
                If strReturnVal = "" Then
                      strReturnVal = adoRecordset.Fields(intCount).Value
                      strReturnVal = strReturnVal & VbCrLf & adoRecordset.Fields(intCount).Value
                End If
          ' Move to the next record in the recordset.
      ' Clean up.
      Get_LDAP_User_Properties = strReturnVal
End Function

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now