Solved

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

Posted on 2007-03-19
19
288 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 125 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 125 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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