[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-04-20
Medium Priority
Last Modified: 2012-05-06

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.

Question by:ShuttleDIK
  • 6
  • 3
LVL 12

Expert Comment

ID: 24190096
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

LVL 12

Expert Comment

ID: 24190109
It should be obvious but just to be clear, you need to change/amend the items on line 32, 38, 39, and 42

Author Comment

ID: 24194331
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!
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 24213927
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?

Author Comment

ID: 24214246
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

LVL 12

Accepted Solution

Krys_K earned 600 total points
ID: 24214481
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. :-)

Author Comment

ID: 24214648
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!!

Author Comment

ID: 24214718
Great!  Just ran the script successfully.

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

Thanks for the awesome assistance!

Author Closing Comment

ID: 31572425
I've been known to 'Fat Finger' code from time to time, too  ;]

Thanks for the assistance, Krystian.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
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…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

830 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