Link to home
Start Free TrialLog in
Avatar of abshipman
abshipman

asked on

Pull a list of computers from AD that have not logged on in the last 4 weeks

Does anyone have a script or freeware program that will pull a list of computers from AD that have not logged on in the last 4 weeks?
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
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
SOLUTION
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
Rereading that, this line should be altered:

If DateDiff("w", dtLastLogin, Now) > 4 Then 'Only proceed if it's been 4 weeks or more since the last login
Avatar of abshipman
abshipman

ASKER

I am getting the following error.

error line 25
char 66
error expected end of statement
Try replacing it with this:

   wscript.echo objRecordSet.Fields("distinguishedName") & " hasn't logged on in over 4 weeks."
error 'loop' without 'do' on Line 28
Actually the If statement wasn't closed properly...replace with this:

Do Until objRecordSet.EOF
  Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName").Value)
  dtLastLogin = objUser.LastLogin
  If Not IsEmpty(dtLastLogin) Then
    If DateDiff("w", Now, dtLastLogin) > 4 Then 'Only proceed if it's been 4 weeks or more since the last login
      wscript.echo objRecordSet.Fields("distinguishedName").Value) & " hasn't logged on in over 4 weeks."
    End If
  End If '<<added this line
  objRecordSet.MoveNext
Loop
I swear I had this working without these errors earlier this morning... : |
Sorry for all the trouble.
Still getting:
error line 25
char 66
error expected end of statement
Just tested this version with no errors...remember to change the domain...

On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set CN = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
CN.Provider = "ADsDSOObject"
CN.Open "Active Directory Provider"
Set cmd.ActiveConnection = CN
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE
 
strDomain = "dc=company,dc=com" 'change this line
 
cmd.CommandText = "SELECT adsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user'"
Set objRS = cmd.Execute
objRS.MoveFirst
Do Until objRS.EOF
  Set objUser = GetObject(objRS.Fields("adsPath").Value)
  dtLastLogin = objUser.LastLogin
  If Not IsEmpty(dtLastLogin) Then
    If DateDiff("w", dtLastLogin, Now) > 4 Then 'Only proceed if it's been 4 weeks or more since the last login
      wscript.echo objUser.CN & " hasn't logged on in over 4 weeks."
    End If
  End If
  objRS.MoveNext
Loop
 
Sorry I went out of town for a couple of days...  this works great.  Is there a way to outpu this to a file?  more points?
I thought this was going to pull computers?
I take it by your post that it's not working as expected?
What happened to "this works great"?  :o)
This one's growing a bit stale on my brain...what's it not doing that you need it to?
It does work great :)  It just pulls the wrong thing.  I need it to pull computers.
abshipman,

if you change the following line:

cmd.CommandText = "SELECT adsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user'"

to

cmd.CommandText = "SELECT adsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='computer'"

It should pull computers that haven't logged on in the past 4 weeks, rather than users.

Great script sirbounty!

:o)

Bartender_1
Thanx - sorry I missed the notif on this one... : \
abshipman,

While I'm sure sirbounty could tell you how to cause the script to write the output to a text file, this may work for you as well:

when executing the script from the command prompt, append the command like this:

cscript <scriptname>.vbs >>lastlogin.txt

That will kick everything the script shows to a file called lastlogin.txt.

A quick question for you sirbounty,
If I was to tell the script to show objects with a date difference > 16, would that correctly show computers/users that haven't logged in in ~4 months? how about if I changed it to 53, would that correctly show someone that hasn't logged in in over a year?

:o)

Bartender_1
Maybe I should have opened my own question for this.....

Would you prefer I do that sirbounty?

Bartender_1
It's fully dependent on the datediff function:

 If DateDiff("w", Now, dtLastLogin) > 4

States: If the difference between "now" and dtLastLogin is greater than 4 "w"eeks...

So, while it's accurate to say "how about if I changed it to 53, would that correctly show someone that hasn't logged in in over a year?", you would be better off using

 If DateDiff("y", Now, dtLastLogin) > 1
Nothing to back it up with, but I believe that route would be a bit more accurate...