Solved

Script to Delete Unknown Accounts on Workstations

Posted on 2009-05-18
5
480 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now