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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am getting the following error.
error line 25
char 66
error expected end of statement
error line 25
char 66
error expected end of statement
Try replacing it with this:
wscript.echo objRecordSet.Fields("disti nguishedNa me") & " hasn't logged on in over 4 weeks."
wscript.echo objRecordSet.Fields("disti
ASKER
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("disti nguishedNa me").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("disti nguishedNa me").Value ) & " hasn't logged on in over 4 weeks."
End If
End If '<<added this line
objRecordSet.MoveNext
Loop
Do Until objRecordSet.EOF
Set objUser = GetObject("LDAP://" & objRecordSet.Fields("disti
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("disti
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.
Sorry for all the trouble.
ASKER
Still getting:
error line 25
char 66
error expected end of statement
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.Connec tion")
Set cmd = CreateObject("ADODB.Comman d")
CN.Provider = "ADsDSOObject"
CN.Open "Active Directory Provider"
Set cmd.ActiveConnection = CN
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscop e") = 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("ad sPath").Va lue)
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
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set CN = CreateObject("ADODB.Connec
Set cmd = CreateObject("ADODB.Comman
CN.Provider = "ADsDSOObject"
CN.Open "Active Directory Provider"
Set cmd.ActiveConnection = CN
cmd.Properties("Page Size") = 1000
cmd.Properties("Searchscop
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("ad
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
ASKER
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?
ASKER
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?
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?
ASKER
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
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
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
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...
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...
If DateDiff("w", dtLastLogin, Now) > 4 Then 'Only proceed if it's been 4 weeks or more since the last login