Using ADSI and VBScript to enumerate local user accounts

03/07/09

Permalink 05:28:33 am, by dave Email , 1008 words, 8011 views   English (US)
Categories: Windows Scripting, Wsh Scripts

Using ADSI and VBScript to enumerate local user accounts

Have you ever needed to look at all the accounts and the account properties on your local computer? I have, and I could not find a good way to get a listing of all the accounts with their populated attributes. So I created a script that will do just that, enumerate the local computer's users and list the populated user attributes.

[More:]

This script gets the name of the local computer using a call to WScript.Network. It then uses ADSI (Active Directory Services Interface) to enumerate the users and list the their populated attributes.

'==========================================================
'==     List Local Users Sample Script
'==
'==           Copyright © 2009, 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 © 2009,
'==        Dave Moats (http://www.davemoats.com/).
'==
'==========================================================

'==========================================================
'== NOTE: watch for wrapped lines and html special
'==        characters in the web representation of this
'==        sample code
'==========================================================

'==========================================================
'==
'== loclaUsers.vbs - a script used to list all the local
'==                    users and their populated attributes
'==
'==========================================================
option explicit

'==========================================================
'== declare the local variables to be used
'==========================================================
dim scriptHost, tmpArr, scriptName

'==========================================================
'== get the name of the interpreter being used to run the
'== script - have to parse it out of the full path that is
'== stored in the fullname property
'==========================================================
scriptHost = wscript.fullname
tmpArr = split( scriptHost, "\" )
scriptHost = tmpArr( ubound(tmpArr) )

if lcase( scriptHost ) <> "cscript.exe" then
wscript.echo "This script must be executed using cscript.exe"
wscript.quit
end if

'==========================================================
'== declare our variables and get the local computer's name
'==========================================================
dim objNet, adsCompPath, compFilter, compObj, userObj

set objNet = createobject("WScript.NetWork")

'==========================================================
'== bind to the local computer using an NT4 type bind
'==========================================================
adsCompPath = "WinNT://" & objNet.ComputerName

set objNet = nothing

compFilter = array("User")
set compObj = getobject(adsCompPath)

'==========================================================
'== apply the filter to the returned object
'==========================================================
compObj.filter = compFilter

wscript.echo "---------------------------------------------------------------"
wscript.echo "---------------------------------------------------------------"
wscript.echo ""

'==========================================================
'== ignore any of the attribute not present errors
'==========================================================
on error resume next

'==========================================================
'== loop through the user displaying the attributes
'==========================================================
for each userObj in compObj

   wscript.echo "AccountDisabled - " & userObj.AccountDisabled
   wscript.echo "AccountExpirationDate - " & userObj.AccountExpirationDate
   wscript.echo "AdsPath - " & userObj.AdsPath
   wscript.echo "BadLoginAddress - " & userObj.BadLoginAddress
   wscript.echo "BadLoginCount - " & userObj.BadLoginCount
   wscript.echo "Class - " & userObj.Class
   wscript.echo "Department - " & userObj.Department
   wscript.echo "Description - " & userObj.Description
   wscript.echo "Division - " & userObj.Division
   wscript.echo "EmailAddress - " & userObj.EmailAddress
   wscript.echo "EmployeeID - " & userObj.EmployeeID
   wscript.echo "FaxNumber - " & userObj.FaxNumber
   wscript.echo "FirstName - " & userObj.FirstName
   wscript.echo "FullName - " & userObj.FullName
   wscript.echo "GraceLoginsAllowed - " & userObj.GraceLoginsAllowed
   wscript.echo "GraceLoginsRemaining - " & userObj.GraceLoginsRemaining
   wscript.echo "GUID - " & userObj.GUID
   wscript.echo "HomeDirectory - " & userObj.HomeDirectory
   wscript.echo "HomePage - " & userObj.HomePage
   wscript.echo "IsAccountLocked - " & userObj.IsAccountLocked
   wscript.echo "Languages - " & userObj.Languages
   wscript.echo "LastFailedLogin - " & userObj.LastFailedLogin
   wscript.echo "LastLogin - " & userObj.LastLogin
   wscript.echo "LastLogoff - " & userObj.LastLogoff
   wscript.echo "LastName - " & userObj.LastName
   wscript.echo "LoginHours - " & userObj.LoginHours
   wscript.echo "LoginScript - " & userObj.LoginScript
   wscript.echo "LoginWorkstations - " & userObj.LoginWorkstations
   wscript.echo "Manager - " & userObj.Manager
   wscript.echo "MaxLogins - " & userObj.MaxLogins
   wscript.echo "MaxStorage - " & userObj.MaxStorage
   wscript.echo "Name - " & userObj.Name
   wscript.echo "NamePrefix - " & userObj.NamePrefix
   wscript.echo "NameSuffix - " & userObj.NameSuffix
   wscript.echo "OfficeLocations - " & userObj.OfficeLocations
   wscript.echo "OtherName - " & userObj.OtherName
   wscript.echo "Parent - " & userObj.Parent
   wscript.echo "PasswordExpirationDate - " & userObj.PasswordExpirationDate
   wscript.echo "PasswordLastChanged - " & userObj.PasswordLastChanged
   wscript.echo "PasswordMinimumLength - " & userObj.PasswordMinimumLength
   wscript.echo "PasswordRequired - " & userObj.PasswordRequired
   wscript.echo "Picture - " & userObj.Picture
   wscript.echo "PostalAddresses - " & userObj.PostalAddresses
   wscript.echo "PostalCodes - " & userObj.PostalCodes
   wscript.echo "Profile - " & userObj.Profile
   wscript.echo "RequireUniquePassword - " & userObj.RequireUniquePassword
   wscript.echo "Schema - " & userObj.Schema
   wscript.echo "SeeAlso - " & userObj.SeeAlso
   wscript.echo "TelephoneHome - " & userObj.TelephoneHome
   wscript.echo "TelephoneMobile - " & userObj.TelephoneMobile
   wscript.echo "TelephoneNumber - " & userObj.TelephoneNumber
   wscript.echo "TelephonePager - " & userObj.TelephonePager
   wscript.echo "Title - " & userObj.Title
   wscript.echo ""
   wscript.echo "---------------------------------------------------------------"
   wscript.echo "---------------------------------------------------------------"
   wscript.echo ""

next

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

Pay close attention to the lines wrapping in this sample, the script does not have any multi-line statements.

I hope this post helped. If it didn't or you have unexpected results, let me know and I will see if I can figure it out.

Dave



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

This post has 35 feedbacks awaiting moderation...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 2

powered by b2evolution free blog software