Solved

Script to Delete Unknown Accounts on Workstations

Posted on 2009-05-18
5
485 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migration path for Window Virtual PC.... 34 139
creating a random password in batch 4 50
Need to export the list of AD users with below attrubutes 2 43
VBA code for SubFolders 11 50
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…

752 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