'====================================================================
'= Show Domain Users 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/).
'=
'====================================================================
'====================================================================
'==
'== listusers - script to list all the users on the specified domains
'==
'====================================================================
set fso = createobject ( "Scripting.FileSystemObject")

'====================================================================
'== get the info required to run the script
'====================================================================
if wscript.arguments.count = 0 then
   wscript.echo ( "Usage: listusers.vbs <Path to Input file> <Path to Ouput file>")
   domainFilePath = inputbox ( "Enter the path to file that contains the domains you want to query:")
   outputPath = inputbox ( "Enter the path to the output file:")
elseif wscript.arguments.item ( 0) = "/?" Or wscript.arguments.item ( 0) = "-?" then
   wscript.echo ( "Usage: listusers.vbs <Path to Input file> <Path to Ouput file>")
   wscript.quit ( )
else
   domainFilePath = wscript.arguments.item ( 0)
   outputPath = wscript.arguments.item ( 1)
end if

'====================================================================
'== Make sure we have some info
'====================================================================
if domainFilePath = "" or outputPath = "" then
   wscript.echo ( "There were not enough arguments passed to the script")
   wscript.echo ( "Usage: listusers.vbs <Path to Input file> <Path to Ouput file>")
   wscript.quit ( )
end if

'====================================================================
'== open the files so we can do some work
'====================================================================
set outputFile = fso.createtextfile ( outputPath)
set domainFile = fso.opentextfile ( domainFilePath)

'====================================================================
'== write the header line to the output file
'====================================================================
outputFile.writeline ( "User Account, Full Name, Comment, Status")

on error resume next

'====================================================================
'== loop thru the file and process the contents
'====================================================================
do while not ( domainFile.atendofstream)

   domainToProcess = domainFile.readline

   '==================================================================
   '== bind to the domain using an NT4 type bind
   '==================================================================
   adPathToQuery = "WinNT://" & domainToProcess
   domFilter = array ( "User")
   set domObj = getobject ( adPathToQuery)

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

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

      outMsg = inline & "," & usr.name

      '================================================================
      '== try get the user's full name and add to our string
      '================================================================
      fname = usr.FullName
      if fname <> "" then
         outMsg = outMsg & "," & fname
      else
         outMsg = outMsg & ","
      end if

      '================================================================
      '== try get the user's description and add to our string
      '================================================================
      cmt = usr.Description
      if cmt <> "" then
         outMsg = outMsg & "," & cmt
      else
         outMsg = outMsg & ","
      end if

      '================================================================
      '== see if the account is disabled and add to our string
      '================================================================
      if usr.AccountDisabled = true then
         outMsg = outMsg & ",Disabled"
      else
         outMsg = outMsg & ",Enabled"
      end if

      outputFile.writeline ( outMsg )

      outMsg = ""

   next

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

   outputFile.writeline ( "" )
   outputFile.writeline ( "" )

loop

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

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.