Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I hope you get this! It took me some time to get.
Copy and paste the below script into notepad and name it something.vbs
^^^^^^^^
Set objUser = GetObject("LDAP://[server_name]/[Distinguished Name of user]")
WScript.Echo objUser.LastLogin
^^^^^^^^
Here's the tricky part. You need to replace the [server_name] with the name of your server. Now you have to come up with a distinguished name for your user object. The best way (I think) to do this is by running the script below. It will give you an example for the currently logged on user.
^^^^^^^^
Set objADSystemInfo = CreateObject("ADSystemInfo")
WScript.Echo objADSystemInfo.UserName
^^^^^^^^
The response you get from this will give you a template to use for the distinguished name of your users. You can tweak it if you want. For example, if it gives you CN=Billy Bob you can change that to CN=bbob if bbob is Billy Bob's username.
The whole idea of the distinguished name is to be sure it's 100% unique in the system, or you'll get errors back from that first script.
In the end, here's an example script for the user account "bbob" on server "Server", from container "Users", in the domain "USA" of billybob.com:
^^^^^^^^
Set objUser = GetObject("LDAP://Server/CN=bbob,CN=Users,DC=USA,DC=billybob,DC=com")
WScript.Echo objUser.LastLogin
^^^^^^^^
I know it's messy, but I hope it helps!
Report Offensive Follow Up For Removal
Thanks alot. It worked just as you said. Is there a way I can get it to generate a list of users & their last login date instead of having to enter each name individually? Thanks again.
Alright!
I got it... almost :) It's not perfect, because for some reason when I tell it to return users, it also return computers as well. Also, if the user has never logged on to the domain it can cause errors, so you've got to have the "On Error Resume Next" statement in there.
OK... Drum roll... Here is a Visual Basic macro to get that info from AD. Just copy and paste it into an Excel macro, customize it for your domain and go for it! (maybe).
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sub GetADInfo()
On Error Resume Next
Dim SQLquery, rs, Conn, I, User
SQLquery = "SELECT cn FROM 'LDAP://server/" & _
"DC=usa,DC=bob,DC = com ' WHERE " & _
"objectClass='User' ORDER BY cn ASC"
Set Conn = CreateObject("ADODB.Connection")
Conn.Provider = "ADSDSOObject"
Conn.Open "ADs Provider"
I = 1
Set rs = Conn.Execute(SQLquery)
Do While Not rs.EOF Or rs.BOF
ReturnValue = rs.Fields(0)
If IsArray(ReturnValue) Then
For I = LBound(ReturnValue) To UBound(ReturnValue)
If ReturnValue(I) "" Then
Cells(I, 1) = ReturnValue(I)
End If
Next
End If
rs.MoveNext
Cells(I, 1) = ReturnValue
I = I + 1
Loop
I = 1
Do While Cells(I, 1) ""
Set User = GetObject("LDAP://server/" & _
"CN=" & Cells(I, 1) & ",CN=Users," & _
"DC=usa,DC=bob,DC=com")
Cells(I, 2) = User.LastLogin
I = I + 1
Loop
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.Font.Bold = True
Cells(1, 1) = "User"
Cells(1, 2) = "LastLogin"
End Sub
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
There you go. Just make sure to change it to suit your domain. Right now it is set for USA.BOB.COM on the LDAP server SERVER in container Users.
Also, here is a link to an MSDN page with all the other things you can get (and set!) using LDAP (see Tables 1 and 2).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnw2kmag01/html/passwords.asp
Hope this helps you and anyone else!
Report Offensive Follow Up For Removal
Well the formatting got a little screwed up. Just make sure that all the lines are put together correctly.
Let me know if this works!
Report Offensive Follow Up For Removal
I get a syntax error when I compile it at:
If ReturnValue(I) "" Then
and also at
Do While Cells(I, 1) ""
Unfortunately, I'm not very fluid in VB. Any suggestions? Thanks alot for your time & help.
Oops. So much for that little coder app. This message board has a hard time with the less than and greater than symbols.
The problem that those two lines should be, changing the )( to the less-than and greater-than signs:
If ReturnValue(I) )( "" Then
and
Do While Cells(I, 1) )( ""
This should work for you. If not, I can send you the Excel file I made and you'll have to change it for your domain.
Report Offensive Follow Up For Removal
I added the less-than & greater-than signs and it got rid of those two syntax errors but when I run it, it appears to get stuck in a loop. I'm guessing that some more of the code got distorted by the message board. Unfortunately, I don't know where to make the corrections at. If it's not too much trouble, I'd really appreciate it if you would email me the Excel file that you created. Thanks for all your help.
Aaron.Boyarko@tri-c.edu
![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |