Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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