'====================================================================
'= User Last Logon VBScript sample - Copyright © 2007, Dave Moats
'=
'= This sample is provided 'as-is', without any express or implied warranty.
'= In no event will the authors be held liable for any damages arising from
'= the use of this sample code.
'=
'= Permission is granted to anyone to use this sample code for any purpose,
'= including commercial applications, subject to the following restrictions:
'=
'= The origin of this code must not be misrepresented;
'=       you must not claim that you wrote the original code.
'=       If you use this code, an acknowledgment in the
'=       documentation is requested - shown below:
'=
'= Portions Copyright © 2007, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'==
'== lastlogon - script to list all the users in a domain, with the
'==                   user's last logon date.
'==
'==    NOTE: this is only valid on domains with fewer than 3 domain
'==             controllers
'==
'====================================================================

'====================================================================
'== create the reference to the file system object
'====================================================================
set fso = createobject ( "Scripting.FileSystemObject")

'====================================================================
'== get the info required to run the script
'====================================================================
if wscript.arguments.count = 0 then
   wscript.echo ( "Usage: lastlogon.vbs <Domain> <Path to Ouput file>")
   domPath = inputbox ( "Enter the domain that you want to get the last logon for:")
   outFilePath = inputbox ( "Enter the path to the output file:")
elseif wscript.arguments.item ( 0) = "/?" Or wscript.arguments.item ( 0) = "-?" then
   wscript.echo ( "Usage: lastlogon.vbs <Domain> <Path to Ouput file>")
   wscript.quit ( )
else
   domPath = wscript.arguments.item ( 0)
   outFilePath = wscript.arguments.item ( 1)
end if

'====================================================================
'== validate that there was something passed to the script
'====================================================================
if domPath = "" or outFilePath = "" then
   wscript.echo ( "There were not enough arguments passed to the script")
   wscript.echo ( "Usage: lastlogon.vbs <Domain> <Path to Ouput file>")
   wscript.quit ( )
end if

'====================================================================
'== open the file that will contain the results of this script
'====================================================================
set outputFile = fso.createtextfile ( outFilePath)

on error resume next

'====================================================================
'== using an NT4 style bind to attach to the domain
'====================================================================
domPath = "WinNT://" & domPath
domFilter = array ( "User")
set domObj = getobject ( domPath)

'====================================================================
'== apply the filter to the returned object
'====================================================================
domObj.filter = domFilter

'====================================================================
'== write the file header line
'====================================================================
outputFile.writeline ( "User Name, Last Login" )

'====================================================================
'== loop through the list of groups displaying the members
'====================================================================
for each usr in domObj

      ll = usr.lastlogin
      if ll <> "" then
         outputFile.writeline ( usr.name & " " & usr.lastlogin )
      else
         outputFile.writeline ( usr.name)
      end if

next

'====================================================================
'== dump the reference to the domain
'====================================================================
set domObj = nothing

'====================================================================
'== clean up the out file reference and the file system object
'====================================================================
outputFile.close
set outputFile = nothing

set fso = nothing

wscript.quit ( )




Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.