Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 868
  • Last Modified:

Script to remove users from local admin group on remote computers

I have a csv file with a list of computers and users that are added to the local Administrators group of those computers and need a script that will read the csv file and remove the user from that particular computer's local Administrator group.

I was able to find a simple script that will remove a stated user from a stated computer (see below) but again I need something that will read the csv file with users that will be removed from certain computers.

Dim DomainName
Dim UserAccount
Set net = WScript.CreateObject("WScript.Network")
local = "computer name"
DomainName = "domain name"
UserAccount = "user name"

set group = GetObject("WinNT://"& local &"/administrators")
on error resume Next
group.Remove "WinNT://"& DomainName &"/"& UserAccount &""

0
desktop-ee
Asked:
desktop-ee
1 Solution
 
jawa29Commented:
Hi desktop-ee

Try this script out, change the variable sCSVFileName to be the full path to the CSV file you have.

This script is untested so please test on one PC before going live.

Jawa29
Const ForReading = 1

On Error Resume Next

sCSVFileName = "C:\CSVFile.csv"

Set oRoot = GetObject("LDAP://RootDSE")
vDomain = Replace(Replace(oRoot.Get("DefaultNamingContext"), ",DC=", "."), "DC=", "")

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFSO.OpenTextFile (sCSVFileName, ForReading)

Do Until oTextFile.AtEndOfStream
	sLine = oTextFile.ReadLine
	If Trim(sLine) <> "" Then
		sPC = Split(sLine, ",")(0)
		sUser = Split(sLine, ",")(1)
		
		Err.Clear
		Set oGroup = GetObject("WinNT://"& sPC &"/administrators")
		If Err.Number = 0 Then
			Err.Clear
			oGroup.Remove "WinNT://"& vDomain &"/"& sUser &""
			If Err.Number = 0 Then
				WScript.Echo sUser & " was removed successfully"
			Else
				WScript.Echo "An error occured - " & Err.Description
			End If
		Else
			WScript.Echo "PC is currently unavailable"
		End If
	End If	 
Loop

Open in new window

0
 
desktop-eeAuthor Commented:
Apologies for the late response. I tried the above script and it worked perfectly in our test environment. We have to do some more testing on a wider range of computers but I believe this is going to work great. Thank you so much Jawa29.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now