[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Local Account Query is giving duplicates WMI script

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

0
Godzirra
Asked:
Godzirra
  • 3
  • 3
1 Solution
 
sirbountyCommented:
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

0
 
GodzirraAuthor Commented:
SirBounty you are the bomb..  thank you so much. :))
0
 
sirbountyCommented:
Glad I could help - thanx for the grade! :^)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GodzirraAuthor 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?  
0
 
sirbountyCommented:
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

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

Have a great day,
Eric
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now