Enumerating local accounts with WMI


G

Guest

I'm trying to enumerate the local accounts on my XP Workstation with the
following code. The goal is to integrate additional code that will look for
a particular account and disable it. When I run this code in a VBS file, it
appears to be spending a long, long time generating the collection of
accounts. My registry impersonation level is set to 3 and I'm running the
script in the context of a domain account that is a local administrator.

Set oUserAccounts = GetObject("winmgmts:").InstancesOf ("Win32_UserAccount")

For Each Account in oUserAccounts
WScript.Echo Account.Name
Next
 
Ad

Advertisements

T

Torgeir Bakken \(MVP\)

Jay said:
I'm trying to enumerate the local accounts on my XP Workstation with the
following code. The goal is to integrate additional code that will look for
a particular account and disable it. When I run this code in a VBS file, it
appears to be spending a long, long time generating the collection of
accounts. My registry impersonation level is set to 3 and I'm running the
script in the context of a domain account that is a local administrator.

Set oUserAccounts = GetObject("winmgmts:").InstancesOf ("Win32_UserAccount")

For Each Account in oUserAccounts
WScript.Echo Account.Name
Next
Hi

You need to specify that the "domain" is the name of the local
computer, like this:

'--------------------8<----------------------
Set objWshNet = CreateObject("WScript.Network")
strComputer = objWshNet.ComputerName ' local computer

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_UserAccount Where Domain = '" & strComputer & "'")

For Each objItem in colItems
....

'--------------------8<----------------------


Alternatively, include the user name in the WQL statement as well:

'--------------------8<----------------------
Set objWshNet = CreateObject("WScript.Network")
strComputer = objWshNet.ComputerName ' local computer

strUser = "tst"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_UserAccount Where Domain='" & strComputer & "'" _
& " And Name='" & strUser & "'")

For Each objItem in colItems
....

'--------------------8<----------------------


But if your task is to disable an user account if it exist, this
is an easier way to do it:

'--------------------8<----------------------
strUser = "SomeUser"

Set objWshNet = CreateObject("WScript.Network")
strComputer = objWshNet.ComputerName ' local computer

On Error Resume Next ' in case the user does not exist.
Set objUser = GetObject("WinNT://" & strComputer & "/" & strUser & ",user")

If Err.Number = 0 Then
objUser.AccountDisabled = True
objUser.SetInfo
End If
On Error Goto 0

'--------------------8<----------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

WMI Local Access denied on Win2k3 15
WMI 0
Wmi 0
Wmi 0
Run WMI within WMI 0
WMI query local computer fails with access denied despite being local admin 0
Enumerating LSA 2
Enumerate Profiles 0

Top