Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use WMI to Enumerate Local User Accounts

515 views
Skip to first unread message

B V

unread,
Nov 12, 2003, 1:40:44 PM11/12/03
to
Hi,

I am looking at Use WMI to Enumerate Local User Accounts
from http://www.microsoft.com/technet/treeview/default.asp?
url=/technet/ScriptCenter/user/ScrUG156.asp and I ran this
script on one of my Windows 2003 Active Directory. I am
receiving a blank results when run this program. Need help
to figure out why this script not working. Do I need to
change LocalAccount = False in order to work? Is there a
DomainAccount = True?


On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer
& "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_UserAccount Where LocalAccount = True")
For Each objItem in colItems
Wscript.Echo "Account Type: " & objItem.AccountType
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Disabled: " & objItem.Disabled
Wscript.Echo "Domain: " & objItem.Domain
Wscript.Echo "Full Name: " & objItem.FullName
Wscript.Echo "Local Account: " & objItem.LocalAccount
Wscript.Echo "Lockout: " & objItem.Lockout
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Password Changeable: " &
objItem.PasswordChangeable
Wscript.Echo "Password Expires: " &
objItem.PasswordExpires
Wscript.Echo "Password Required: " &
objItem.PasswordRequired
Wscript.Echo "SID: " & objItem.SID
Wscript.Echo "SID Type: " & objItem.SIDType
Wscript.Echo "Status: " & objItem.Status
WScript.echo "mail: " & objItem.Mail
Wscript.Echo
Next

Thorpe

unread,
Nov 13, 2003, 12:11:35 PM11/13/03
to
If you are running this on a DC all accounts will have false for
LocalAccount as all accounts will be domain accounts

I just did a test script that work as follows

objItem.Caption will format like so - domain\name
objItem.name will format like so - name

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount")


For Each objItem in colItems
Wscript.Echo "Account Type: " & objItem.AccountType
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo

Next

"B V" <bve...@hotmail.com> wrote in message
news:031301c3a94c$7af79380$a101...@phx.gbl...

B V

unread,
Nov 13, 2003, 8:46:18 PM11/13/03
to
Hi,

Thank you and I will give that a try. I'll let you know
what the result will be. Thanks.

>.
>

B V

unread,
Nov 13, 2003, 9:07:57 PM11/13/03
to
Thank you Thorpe. That work. How do I import a file? For
example, if I have a file called, "users_info.txt", how do
I write a script that will import those informations? Do
it read 1 line at a time or do it need to be all on 1 line
with a comma?.. Sample of users_info.txt.....

AccountType: 512
Caption: Guest
Description: Built-in account for guest
Full Name: Guest, My
Email: gu...@mydomain.com

Any help will be appreciated. Can you explain how your
script work when import users_info.txt? I just wanted to
make sure users_info.txt contain the right format for the
script to run properly. Thank you.

>.
>

Thorpe

unread,
Nov 14, 2003, 3:59:56 AM11/14/03
to
Not sure If I know what you are trying to do
Are you tring to create users via script using a txt file with all the new
users in the users_info.txt
or are you trying to list all of the current users and put them into a text
file?

to read or create a text file you need to use the Script Runtime
There is an object called the FileSystemObjectHave a look here at how it
works
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsobjfilesystem.asp

once you have a file then you can use the WMI to file in the details you
want eg Caption: Guest
Can you explain what you are trying to do and I may be able to help further.


"B V" <bve...@hotmail.com> wrote in message

news:056001c3aa54$1f442400$a401...@phx.gbl...

B V

unread,
Nov 14, 2003, 12:24:36 PM11/14/03
to
Hi,

Thank you for getting back to me. Very appreciated.
Apologize for the confusion. What I wanted is to create a
user according to the users_info.txt and if that user
already exist on my Domain, then it will update whatever
field it is missing, eg. Description field, Full Name,
Email, etc.

When I ran the script after you assisted me, the screen
displayed Caption and other things but I tried
objItem.Mail, it told me that this object is not
available, unless I am using the wrong Object type.

I hope I explain to you correctly.

Thank you

>.
>

Thorpe

unread,
Nov 17, 2003, 9:47:39 AM11/17/03
to
as you are using active directory the best way to deal with user that are
part of the Active directory is to use ADSI not WMI
WMI is about management of PC susch as Harware or software applications. WMI
does not implement all the information you
need for the users e.g. email.

Active directory services interfaces (ADSI) is a way to program/script items
that are held in your Active directory e.g. users

to get the email of a user in your AD you could do this

Set objUser = GetObject("LDAP://CN=John Smith,OU=Department,DC=domain,
DC=com")
strMail = objUser.Get("mail")
WScript.Echo strMail

you will need to read in user.txt (FileSystemObject) and then use ADSI to
create the users in your AD or fill in items such as email
have a look here for more on ADSI

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netdir/adsi/adsi_objects_of_ldap.asp

hope this helps

"B V" <bve...@hotmail.com> wrote in message

news:423c01c3aad4$2d1056a0$a601...@phx.gbl...

B V

unread,
Nov 17, 2003, 12:48:48 PM11/17/03
to
Hi,

Thank you for your assistant and the informations. I will
give that a try.

I had used a script for inputting Email from one of the
Newsgroup. Do you know how to fix the Email Field (since
no one has resolved his issue) that only display the email
address and not also display the username? For example, in
my user_info.txt, I have something like this.....

us...@mydomain.com, us...@yahoo.com

In the Email Field, it will display both as
us...@mydomain.com, us...@yahoo.com. How do I re-modify
the script where the Email Field will only display
us...@yahoo.com and not us...@mydomain.com?

Once again, thank you for your kindness.

>Option Explicit
>Dim objRootDSE, objTrans, objFSO, objFile, objUser
>Dim strDNSDomain, strNetBIOSDomain, strFileName
>Dim strEmail, strNTName, strUserDN
>
>' Specify the file of email addresses.
>strFileName = "c:\MyFolder\emails.txt"
>
>' Open the file.
>Set objFSO = CreateObject("Scripting.FileSystemObject")
>Set objFile = objFSO.OpenTextFile(strFileName, 1)
>
>' Determine DNS domain name from RootDSE object.
>Set objRootDSE = GetObject("LDAP://RootDSE")
>strDNSDomain = objRootDSE.Get("defaultNamingContext")
>
>' Use the NameTranslate object to find the NetBIOS domain
>name from the
>' DNS domain name.
>Set objTrans = CreateObject("NameTranslate")
>objTrans.Init 3, strDNSDomain
>objTrans.Set 1, strDNSDomain
>strNetBIOSDomain = objTrans.Get(3)
>' Remove trailing backslash.
>strNetBIOSDomain = Left(strNetBIOSDomain, Len
>(strNetBIOSDomain) - 1)
>
>' Use NameTranslate to convert NT names to Distinguished
>Names.
>objTrans.Init 1, strNetBIOSDomain
>
>' Read each line of the file.
>Do Until objFile.AtEndOfStream
> strEmail = Trim(objFile.ReadLine)
> If strEmail <> "" Then
> ' Extract the user NT name from the Email address.
> strNTName = Mid(strEmail, 1, InStr(strEmail, "@") - 1)
> ' Convert the NT name to the Distinguished Name.
> objTrans.Set 3, strNetBIOSDomain & "\" & strNTName
> strUserDN = objTrans.Get(1)
> ' Bind to the user object.
> Set objUser = GetObject("LDAP://" & strUserDN)
> ' Assign the mail attribute.
> objUser.Put "mail", strEmail
> objUser.SetInfo
> End If
>Loop
>
>' Clean up.
>objFile.Close
>Wscript.Echo "Done"

Thorpe

unread,
Nov 18, 2003, 4:14:51 AM11/18/03
to
you need to change the loop

the old loop does this

Do Until objFile.AtEndOfStream
> > strEmail = Trim(objFile.ReadLine)
> > If strEmail <> "" Then
> > ' Extract the user NT name from the Email address.
> > strNTName = Mid(strEmail, 1, InStr(strEmail, "@") - 1)
> > ' Convert the NT name to the Distinguished Name.
> > objTrans.Set 3, strNetBIOSDomain & "\" & strNTName
> > strUserDN = objTrans.Get(1)
> > ' Bind to the user object.
> > Set objUser = GetObject("LDAP://" & strUserDN)
> > ' Assign the mail attribute.
> > objUser.Put "mail", strEmail
> > objUser.SetInfo
> > End If
> >Loop

strEmail will always = us...@mydomain.com, us...@yahoo.com as they are on
the same line and all strEmail holds is a line of text.

strNTName = Mid(strEmail, 1, InStr(strEmail, "@") - 1) will always try and
find the first @ and pull of the first part e.g.
if you line is us...@mydomain.com, us...@yahoo.com then strNTName will =
user1 (this is from us...@mydomain.com)

you need to change strEmail to get just one email address like so
strEmail = Mid(strEmail, 1, InStr(strEmail, ",") - 1)
us...@mydomain.com, us...@yahoo.com will become us...@mydomain.com

then you can do what you like to the new version of strEmail such as get
the part before the @ or just add it to you active directory with

objUser.Put "mail", strEmail
objUser.SetInfo

hope this helps
thanks

"B V" <bve...@hotmail.com> wrote in message

news:007901c3ad33$0daaa390$a401...@phx.gbl...

B V

unread,
Nov 18, 2003, 8:12:34 PM11/18/03
to
Thank you. I had tried what you suggested and it seem that
username has to be the same (us...@mydomain.com,
us...@yahoo.com). If it is different, then it will not
work. The way how I did it right now in my *.txt file, I
have it.....

user1, te...@yahoo.com

In the Email field, it display user1, te...@yahoo.com.
Now, I guess I need to write another script to remove
user1 and keep te...@yahoo.com in the Email field. sorry I
did not explain to you correctly. Wonder if you know how
to write that script. If not, then I will figure it out
myself. Thank you for assisting me. Your suggestion help
me. Thank you so much.

>.
>

0 new messages