?
Solved

vbscript to check accounts that have been inactive for 90 days or more

Posted on 2008-11-05
9
Medium Priority
?
1,769 Views
Last Modified: 2012-05-05
Hi Experts:
I need a vbscript that can check accounts that have been inactive for 90 days or more on a local server.
The server is not a member of any domain.

Could anyone help me with this ?

Thanks

0
Comment
Question by:changjia
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 1200 total points
ID: 22890837
Paste the script below into a text file with a .vbs extension.  Customize the value of the strComputer variable with the name of the target server.  Running it will echo the names of any local users who haven't logged in within 90 days.


On Error Resume Next
 
strComputer = "servername"
intDays = 90
 
Set colAccounts = GetObject("WinNT://" & strComputer & "")
colAccounts.Filter = Array("user")
 
For Each objUser In colAccounts
    If DateDiff("d", objUser.LastLogin, Now) >= intDays Then
        WScript.Echo objUser.Name
    End If
Next

Open in new window

0
 
LVL 14

Assisted Solution

by:rejoinder
rejoinder earned 800 total points
ID: 22890958
This should work for you.
On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
wscript.echo strComputer
Set colUsers = GetObject("WinNT://" & strComputer)
colUsers.Filter = Array("user")
For Each objUser in colUsers
    strADSPath   = "WinNT://" & strComputer & "/" & objUser.Name & ",user"
    Set objVar   = GetObject(strADSPath)
    strLastLogin = objVar.LastLogin
    if err.number <> 0 then
        strLastLogin = "Not on record"
        err.clear
    end if
    if IsDate(strLastLogin) then
        if DateDiff("d",strLastLogin,Date) > 90 then
            wscript.echo vbTab & objUser.Name & vbTab & strLastLogin
        end if
    else
        wscript.echo vbTab & objUser.Name & vbTab & strLastLogin
    end if
Next

Open in new window

0
 

Author Comment

by:changjia
ID: 22891617
Thanks for the help!
Shift-3,I tried your script but nothing happens.
Rejoinder, your script creates pop windows, is there a way to put them in a text file instead of creating pop ups?

Thanks
0
Industry Leaders: 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:changjia
ID: 22891646
Hi Shift-3, Sorry that I put the wrong computer name, your script works great now. but Could you modify the script so it creates a txt file instead of pop ups?

Thanks
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 22891851
So revised.


Const ForWriting = 2
 
strComputer = "servername"
intDays = 90
strLog = "log.txt"
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLog = objFSO.OpenTextFile(strLog, ForWriting, True)
 
Set colAccounts = GetObject("WinNT://" & strComputer & "")
colAccounts.Filter = Array("user")
 
For Each objUser In colAccounts
    If DateDiff("d", objUser.LastLogin, Now) >= intDays Then
        objLog.WriteLine objUser.Name
    End If
Next
 
objLog.Close

Open in new window

0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22892662
OK - this will output the results to a file.
On Error Resume Next
 
strFile     = "Accounts-Over-90-Days.txt"
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, 2, True)
 
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
objFile.WriteLine strComputer
Set colUsers = GetObject("WinNT://" & strComputer)
colUsers.Filter = Array("user")
For Each objUser in colUsers
    strADSPath   = "WinNT://" & strComputer & "/" & objUser.Name & ",user"
    Set objVar   = GetObject(strADSPath)
    strLastLogin = objVar.LastLogin
    if err.number <> 0 then
        strLastLogin = "Not on record"
        err.clear
    end if
    if IsDate(strLastLogin) then
        if DateDiff("d",strLastLogin,Date) > 90 then
            objFile.WriteLine vbTab & objUser.Name & vbTab & strLastLogin
        end if
    else
        objFile.WriteLine vbTab & objUser.Name & vbTab & strLastLogin
    end if
Next

Open in new window

0
 

Author Comment

by:changjia
ID: 22895861
You two are awesome!! Great Thanks to both of you.
I have to give Shift-3 a little more points because he responded faster.
But both of you are really GREAT!!
0
 

Expert Comment

by:Hobbs13
ID: 24787745
Ok, how does this script work?

I ran the script from one of my Domain Controllers (We have 2 total) and replaced strComputer with one of my DC Names. According to this script 1/3rd of of my users have not logged in over last 90 days. This is not possible so I figure I am doing something wrong. Any thoughts?

0
 
LVL 38

Expert Comment

by:Shift-3
ID: 24790706
This script only checks the activity of local users.  It won't work for domain users.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 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