?
Solved

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

Posted on 2007-03-19
19
Medium Priority
?
296 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:abshipman
  • 10
  • 6
  • 3
19 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 375 total points
ID: 18749524
Try this...

On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

strDomain="dc=company,dc=com"

objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
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
  objRecordSet.MoveNext
Loop
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 375 total points
ID: 18749531
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18749600
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:abshipman
ID: 18750242
I am getting the following error.

error line 25
char 66
error expected end of statement
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18750261
Try replacing it with this:

   wscript.echo objRecordSet.Fields("distinguishedName") & " hasn't logged on in over 4 weeks."
0
 

Author Comment

by:abshipman
ID: 18751701
error 'loop' without 'do' on Line 28
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18751717
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18751724
I swear I had this working without these errors earlier this morning... : |
Sorry for all the trouble.
0
 

Author Comment

by:abshipman
ID: 18755906
Still getting:
error line 25
char 66
error expected end of statement
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18756130
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
 
0
 

Author Comment

by:abshipman
ID: 18793311
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?
0
 

Author Comment

by:abshipman
ID: 18793375
I thought this was going to pull computers?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18796187
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?
0
 

Author Comment

by:abshipman
ID: 18801775
It does work great :)  It just pulls the wrong thing.  I need it to pull computers.
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 18885060
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18885101
Thanx - sorry I missed the notif on this one... : \
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 18885389
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
0
 
LVL 22

Expert Comment

by:Christopher McKay
ID: 18885394
Maybe I should have opened my own question for this.....

Would you prefer I do that sirbounty?

Bartender_1
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18885433
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...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question