Solved

Script to Delete Unknown Accounts on Workstations

Posted on 2009-05-18
5
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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