We help IT Professionals succeed at work.

Local Account Query is giving duplicates WMI script

Medium Priority
511 Views
Last Modified: 2013-12-24
Hello,
I must first preface this with the fact that I am a novice scripter.  
I put together this script to go throughout our environment to find any user accounts that don't fit our predefined users in our network.  The script is only for local accounts (the A.D. query was much easier).  but I am hitting a snag.  Here is what I have found.  
The script calls a list of computers in the computers.txt file, if it hits a computer that is non-microsoft or doesn't adhere to wmi, then it repeats the command off of the last computer in the list.  for instance, if I have a computer list of:
computer1
computer2
computer3
computer4
and computer 3 and 4 are not windows based, the script gives me the user accounts three times (once for computer2, and twice for each bogus name it hits) in my output.  I can't seem to get rid of this duplication.  any ideas?  
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\computers.txt", 1)
   
Do Until objTextFile.AtEndOfStream
 strComputer = objTextFile.ReadLine
  
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 
Set colItems = objWMIService.ExecQuery( _
    "SELECT Domain, Name FROM Win32_UserAccount WHERE LocalAccount = True",,48) 
 
	For Each objItem in colItems 
if objItem.Name <> "Administrator" AND _	
	objItem.Name <> "Guest" AND left(objItem.Name ,4) <> "IUSR" AND left(objItem.Name ,6) <> "SUPPOR" _
AND objItem.Name <> "ASPNET" AND objItem.Name <> "testuser" AND objItem.Name <> "HelpAssistant" 	_
AND objItem.Name <> "SQLDebugger" AND left(objItem.Name ,4) <> "IWAM" Then 
 
   Wscript.Echo objItem.Name & "," & objItem.Domain  
   ' & "," & objItem.FullName & "," & objItem.SID
	'Wscript.Echo ","
	'Wscript.Echo "Domain: " & objItem.Domain 
End if 	
 
 
 Next
 Loop
WScript.Quit

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007
Commented:
Try this...

*It helps to remove the first line (Or Error) until you have all the 'kinks' worked out as it can easily mask problems and cause unpredictable results...
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objTextFile = objFSO.OpenTextFile ("c:\scripts\computers.txt", 1)
   
Do Until objTextFile.AtEndOfStream
 strComputer = objTextFile.ReadLine
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 Set colItems = objWMIService.ExecQuery( _
    "SELECT Domain, Name FROM Win32_UserAccount WHERE LocalAccount = True",,48) 
 If Err.Number <> 0 Then
    wscript.echo "Could not connect to computer " & strComputer
 Else
   For Each objItem in colItems 
if objItem.Name <> "Administrator" AND _        
        objItem.Name <> "Guest" AND left(objItem.Name ,4) <> "IUSR" AND left(objItem.Name ,6) <> "SUPPOR" _
AND objItem.Name <> "ASPNET" AND objItem.Name <> "testuser" AND objItem.Name <> "HelpAssistant"         _
AND objItem.Name <> "SQLDebugger" AND left(objItem.Name ,4) <> "IWAM" Then 
 
   Wscript.Echo objItem.Name & "," & objItem.Domain  
   ' & "," & objItem.FullName & "," & objItem.SID
        'Wscript.Echo ","
        'Wscript.Echo "Domain: " & objItem.Domain 
End if  
 
 Next
End If 
Loop
WScript.Quit

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
SirBounty you are the bomb..  thank you so much. :))
CERTIFIED EXPERT
Top Expert 2007

Commented:
Glad I could help - thanx for the grade! :^)

Author

Commented:
I have a caveat to your code though.  When I run it through, it now starts skippling live systems?  If I take the if statement out then it works through all of the machines, if I put it back in, it skips live systems.  
Any ideas?  
CERTIFIED EXPERT
Top Expert 2007

Commented:
need to determine the actual error being raised - could be multiple errors actually...
I'd advise checking against some systems that fall in both categories - without the on error statement.
Otherwise - try this to report the error description:
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objTextFile = objFSO.OpenTextFile ("c:\scripts\computers.txt", 1)
   
Do Until objTextFile.AtEndOfStream
 strComputer = objTextFile.ReadLine
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 Set colItems = objWMIService.ExecQuery( _
    "SELECT Domain, Name FROM Win32_UserAccount WHERE LocalAccount = True",,48) 
 If Err.Number <> 0 Then
    wscript.echo "Could not connect to computer " & strComputer
    msgbox Err.Description '''added this line
 Else
   For Each objItem in colItems 
if objItem.Name <> "Administrator" AND _        
        objItem.Name <> "Guest" AND left(objItem.Name ,4) <> "IUSR" AND left(objItem.Name ,6) <> "SUPPOR" _
AND objItem.Name <> "ASPNET" AND objItem.Name <> "testuser" AND objItem.Name <> "HelpAssistant"         _
AND objItem.Name <> "SQLDebugger" AND left(objItem.Name ,4) <> "IWAM" Then 
 
   Wscript.Echo objItem.Name & "," & objItem.Domain  
   ' & "," & objItem.FullName & "," & objItem.SID
        'Wscript.Echo ","
        'Wscript.Echo "Domain: " & objItem.Domain 
End if  
 
 Next
End If 
Loop
WScript.Quit

Open in new window

Author

Commented:
I will do so, thank you for all of the help and advice.

Have a great day,
Eric
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.