Solved

Script to Delete Unknown Accounts on Workstations

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

939 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

10 Experts available now in Live!

Get 1:1 Help Now