Link to home
Start Free TrialLog in
Avatar of IT_Resource
IT_Resource

asked on

the last logon timestamp for a user

' This VBScript code prints the last logon timestamp for a user.

' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' ISBN: 0-596-00466-4
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strUserDN="cn=username, ou=usrou, dc=name01.pa.domain, dc=org"  

' ------ END CONFIGURATION ---------

set objUser = GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#

I tried using the above VB script I found on the internet and tried to use it to get the last time a given user logged into the domain, this way I can find out if I can change the password on the user without risking it might be used somewhere.

My Domain is pretty big we have a main domain with several child domains to it

When I run the script this is what I get

Line:13
Char:1
Error: A referral was returned from the server
Code: 8007202B
Source: (null)

Thanks

error.jpg
ASKER CERTIFIED SOLUTION
Avatar of exx1976
exx1976
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IT_Resource
IT_Resource

ASKER

i'm getting the same error using the above code
question
where would it be the safe to run this script from? on the DC or WS?
SOLUTION
Avatar of rejoinder
rejoinder
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
From anywhere in the domain, as long as the user running it has permissions to read those attributes from AD.

Please paste the exact line that's giving you an error.
D'oh!   Yeah, I missed that..

You have to have the correct DN in order for this to work.

LOL

Good catch rejoinder
ok let's try this a little different
here is what I get on the Object tab

pa.domain.org/ABC/ABC Conversion

if I pulg that in shoudn't it look like this:

strUserDN="cn=ABC Conversion,ou=ABC,dc=pa.domain,dc=org"  
set objUser = GetObject("LDAP://" & strUserDN)
wscript.echo objUser.lastlogin

well if that's right it's still giving me the same error

Make it look like this;
strUserDN="cn=ABC Conversion,ou=ABC,dc=pa,dc=domain,dc=org"  
Nope.  Use this


strUserDN = "CN=ABC Conversion,OU=ABC,DC=pa,DC=domain,DC=org"
set objUser = GetObject("LDAP://" & strUserDN)
wscript.echo objUser.lastlogin

Open in new window

WTF!!!
check what I get... lol...
vb2.jpg
Ok, what is line 3?  Are there only 3 lines in the vbs file you're using?  Please post line 3.

Most likely there's a fatfinger in the DN..
set objUser = GetObject("LDAP://" & strUserDN)
If that line is the 3rd line then we have to go back to the distinguished name you are entering above.
Basically the script cannot find the user name you are entering.  Perhaps there is a longer OU path tha is missing?  If so, please come clean and lets see the users full path so we can help sort this out.
let me show you
pic.jpg
Great - so I take it your script looks exactly like this...
If the script still does not work, then the OU PGM may be a container rather than an OU in which case you would need to use this as line 1;
strUserDN = "CN=PGM Conversion,CN=PGM,DC=na,DC=bpww,DC=org"
strUserDN = "CN=PGM Conversion,OU=PGM,DC=na,DC=bpww,DC=org"
set objUser = GetObject("LDAP://" & strUserDN)
wscript.echo objUser.lastlogin

Open in new window

strUserDN = "CN=PGM Conversion,OU=PGM,DC=na,DC=bpww,DC=org"

Use that.
ok it's getting better
here is what I'm getting

onemore.jpg
That means the account has never logged on...   ??
Rock on.
you guys are good at this.
how would you say it's fair to split some points here?
Hey this is all exx1976's script, I'd be happy with anything :-)
Keep it up guys
Good Work