Solved

Script to Delete Unknown Accounts on Workstations

Posted on 2009-05-18
5
483 Views
Last Modified: 2013-11-05
I am trying to clean up the remnants of these accounts from all the workstations (I do not want to assume that it was all in the admin group, may be a db2 local group on the machine). So I want something that will recognize that the account no longer exists. The code snippet would be fine if there was a way to have it search through all local groups (not just administrators) and the environment is too large to try and find out exactly what groups are on which machines.

Is there a way to query AD for valid accounts and delete invalid?
Is it ok to search for a partial SID ("S-1-") as below.

Thanks for the help.

Domain = WScript.Arguments.Item(0) 
	Server = WScript.Arguments.Item(1) 
	Set objAdministrators = GetObject("WinNT://" & Domain & "/" & Server &  "/Administrators,group") 
	Set Administrators = objAdministrators.Members 
	For Each Admin in Administrators 
	If Mid(Admin.Name,1,4) = "S-1-" then 
	WScript.Echo "Removing " & Admin.Name 
	objAdministrators.Remove Admin.AdsPath 
	End If 
	Next

Open in new window

0
Comment
Question by:sfarazmand
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Jared Luker earned 500 total points
ID: 24414588
This works on my computer and would be great in a startup script.  When the computer processed the startup script, it should pull out all of those SID's out of there.
'Server = WScript.Arguments.Item(0) 
'strComputer = Server
strComputer = "."
Set colGroups = GetObject("WinNT://" & strComputer & "")
colGroups.Filter = Array("group")
For Each objGroup In colGroups
    'Wscript.Echo objGroup.Name 
    For Each objUser in objGroup.Members
        'WScript.Echo vbTab & objUser.Name
        If Mid(objUser.Name,1,4) = "S-1-" then 
			WScript.Echo "Removing " & objGroup.Name & " " & objUser.Name
			'WScript.Echo objUser.AdsPath
			objGroup.Remove objUser.AdsPath
		End If 
    Next
Next

Open in new window

0
 
LVL 7

Author Comment

by:sfarazmand
ID: 24415255
Thanks. I'll give it a shot.
0
 
LVL 7

Author Comment

by:sfarazmand
ID: 24442758
Works Like a charm. Thanks.
0
 
LVL 7

Author Closing Comment

by:sfarazmand
ID: 31582670
Perfect!!
0
 
LVL 17

Expert Comment

by:Jared Luker
ID: 24442966
Good deal!  Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question