VB or Batch script to add Domain Users to local Power Users group


I've tried searching for an answer on this and found some useful material, but nothing that addresses my problem exactly.  I was hoping someone could direct me further.

I would like to run a script that goes accross the network and adds "Domain Users" to the local "Power Users" group on selected PCs.  I don't need a true loop function, though that wouldn't hurt.  I don't have a problem hard coding the computer names.

i guess my main questions are syntax and whether VBS or straight batch.

Thanks so much for your help.

Who is Participating?
Hi there
I will respond better later as i'm off out but just quickly, the error in your last post is because
arrComputers - Split(strResults, vbNewLine)

should be
arrComputers = Split(strResults, vbNewLine)
Notice the = sign. Change it to that and try again. i have to say, i wasn't able to test this script so apologies for that.
I will help you resolve the other errors later. :-)
Hi there
I've written a VBScript that should do what you want.
It takes a list of computers from a file (1 per line) and uses that to go to the computer and add the AD group to the Local computers group (I've specified the names of the groups but check them)
Things to note:
  • Specify the filename and path
  • Specify the Group Names
  • Specify the Domain Name
  • Computer names 1 per line in the file
Copy the whole script to notepad and save as a .vbs
Open a command prompt and run the script like
 cscript.exe scriptname.vbs
Hope it's what you are looking for :-)

Option Explicit
' Start the script
	Call GetComputerList
' Tell us when its complete
		WScript.Echo "Complete!"
Sub GetComputerList()
' Version 1.0
' Written by Krystian Karia
' Dated 21-Apr-2009
' Gets a list of computers from a file
' specified  and creates  an array  to 
' then loop each one and call  the sub
' passing the ComputerName, DomainName
' and both Group Names specified below
' Declare variables and constants
 	Dim fso, oFile
	Dim arrComputers
	Dim sDomain
	Dim sGlobalGrp, sLocalGrp
	Dim strResults
	Dim i
	Const ForReading = 1
	Const sFileLocation = "C:\ComputerList.txt" ' <_______Change to suit
' **********************************************************************
' Global and Local Group Name
		sGlobalGrp = "Domain Users"
		sLocalGrp = "Power Users"
' NetBIOS Name of Domain
		sDomain = "MyDomainName"		' i.e. MyDomain.Com = MyDomain
' **********************************************************************
' Create Objects and open file for reading
	Set fso = CreateObject("Scripting.FileSystemObject")
' Open File for reading
	Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
		If Err.Number <> 0 Then
			WScript.Echo "Error reading File " & sfileLocation
		End If
' Read the whole contents of the File
	Do While oFile.AtEndOfStream <> True
		strResults = oFile.ReadAll
' Close the file and release the object from memory
	Set oFile = Nothing
' Create an array of compuiters from the file
	arrComputers - Split(strResults, vbNewLine)
		strResults = ""
' Loop Computers
	For i = 0 To Ubound(arrComputers)
		If arrComputers(i) = "" Then
			Exit For	' Catches end of file blank line if present
		End If
		' Call the Procedure passing each of the above aprameters
		Call ADGrpToLocalGrp(arrComputers(i), sGlobalGrp, sLocalGrp, sDomain)
	Next ' i
End Sub ' GetComputerList
Sub ADGrpToLocalGrp(strComputer, strGlobalGrp, strLocalGrp, strDomain)
' Version 1.0
' Written by Krystian Karia
' Dated 21-Apr-2009
' Script to take some  parameters and add the AD global
' group to a  local group of  the specified machine. It 
' checks if the groups exists first so as not to do any
' uneccessary work.
' Catch errors ourselves
	On Error Resume Next
' Declare variables
	Dim fso, objGroup, objGrpMember, objGlobalGrp
	Dim bolGrpExists
' Create Objects
	Set fso = CreateObject("Scripting.FileSystemObject")
' Bind to Local Group
	Set objGroup = GetObject("WinNT://" & strComputer & "/" & strLocalGrp & ",group")
' Check no error occurs
	If Err.Number = 0 Then
		bolGrpExists = False
' Check if global group exists in the local group
	    For Each objGrpMember in objGroup.Members
	        If Instr(Ucase(objGrpMember.Name), Ucase(strGlobalGrp)) > 0 Then
				bolGrpExists = True
					WScript.Echo "Group already exists on " & strComputer
				Exit For
			End If
		Next ' objGrpMember
' Add the global group if it doesn't already exist
			If bolGrpExists = False Then
				Set objGlobalGrp = GetObject("WinNT://" & strDomain & "/" & strGlobalGrp & ",group")
					If Err.Number <> 0 Then
						WScript.Echo "There was a problem addin the AD group: '" & _
							strGlobalGrp & "' to the local group: " & strLocalGrp
						Exit Sub
					End If 
			End If 
	Else ' Err
		WScript.Echo "There was a problem binding to the local group: " & strLocalGrp
	End If ' Err
	If Err.Number = 0 And bolGrpExists = False Then 
		WScript.Echo "Successfully added the Global Group: '" & _
			strGlobalGrp & "' to the local group: " & strLocalGrp
	End If 
End Sub

Open in new window

It should be obvious but just to be clear, you need to change/amend the items on line 32, 38, 39, and 42
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ShuttleDIKAuthor Commented:
Wow!  That's awesome.  Thank you so much for such a complete addressing of my request.

I'll test it out tomorrow morning and let you know if I run into any trouble.  It doesn't look like I will, though.

i was thinking about running a script to reverse the assignments, but with the problem I'm addressing, it looks like manual reversal of the change on a 'per case' basis is actually the way to go.

Thanks again for your thorough assistance!
ShuttleDIKAuthor Commented:
I was able to test the script today and I immediately ran into an error:

"(52, 2)  Microsoft VBScript runtime error: ActiveX component can't create object: 'Scripting.FileSystemObject'"

Any ideas?
ShuttleDIKAuthor Commented:
Ok, I researched that error some and for the most part it seems to show that Scripting isn't fully or properly installed on the machine(s) on which I was running the script.

I have tried it on a third machine and am not getting that error - HOWEVER (isn't there always one of these??)  I'm now getting :
    ChgLGrp.vbs(68, 2) Microsoft VBScript runtime error: Type mismatch

The section of script pertaining to the error is in the snippet.  It seems as though the engine has a problem with the ("") designation.
' Create an array of compuiters from the file
	arrComputers - Split(strResults, vbNewLine)
		strResults = ""

Open in new window

ShuttleDIKAuthor Commented:
That makes sense  ;]

And since I'm not getting the Object error on this workstation, let's wait on that error.  I don't need to go around installing scripting updates if I have a machine that will run it...   ;]

Thanks again!!
ShuttleDIKAuthor Commented:
Great!  Just ran the script successfully.

I added the computer name to the confirmation Echo, too.

Thanks for the awesome assistance!
ShuttleDIKAuthor Commented:
I've been known to 'Fat Finger' code from time to time, too  ;]

Thanks for the assistance, Krystian.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.