?
Solved

Adding domain user to local administrators group via script

Posted on 2010-08-18
7
Medium Priority
?
580 Views
Last Modified: 2012-05-10
Hi I want to automate a little chore. I have to add a small number of domain users to the local administrators group on a regular basis. This little script does exactly what I need except I have to enter the COMPUTERNAME. Instead I would like it to run on the machine currently on, I will execute this script on the physical machine, no logon scripts or GPO's involved, just off a thumbdrive or something.

strComputer = "COMPUTERNAME"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)
strComputer = "COMPUTERNAME"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)

So I want this to run without the need to enter the COMPUTERNAME string.

oh source:

http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/08/how-can-i-add-a-domain-user-to-a-local-administrators-group.aspx

0
Comment
Question by:it_techie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 33465426
Try this:

Set objShell = CreateObject( "WScript.Shell" )
strComputer = objShell.ExpandEnvironmentStrings("%ComputerName%")
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)
strComputer = "COMPUTERNAME"
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)

Open in new window

0
 
LVL 2

Accepted Solution

by:
rbartczak earned 1000 total points
ID: 33465445
local computer strComputer = "."
0
 
LVL 1

Expert Comment

by:rickardc
ID: 33465510
I do exactly the same.  This is the script I use via a logon script, but it will work equally well by just running it from the machine in question
on error resume next
'option explicit
dim hostname
dim LocalGroup
dim DomainUsername(1)
dim DomainGroupName(1)
dim Domain

LocalGroup="Administrators"									'The local group you need to add users to
DomainName="XXXXXXX ENTER YOUR DOMAIN HERE XXXXXXXXX"										'NETBIOS name of the domain where to get the domain users and groups
'*********************************************************************************************
'												Add as many domain users or groups to these arrays
DomainUsername(1)=""

DomainGroupName(1)="Domain Users"
'DomainGroupName(2)="Contractors"
'*********************************************************************************************

set ws = Wscript.Createobject ("Wscript.Shell")							'Get the hostname of the local PC
Hostname = ws.ExpandEnvironmentStrings("%COMPUTERNAME%")


set ADGroup = GetObject ("WinNT://" & hostname & "/Administrators,group")			'get the local admin group
if err.number <> 0  then
	'msgbox err.description
else
	'msgbox "Got local group"
end if

for i = 1 to ubound(DomainGroupName)								'Add the domain group(s)
	if DomainGroupName(i) <> "" then								
		adgroup.add ("winnt://" & DomainName & "/" & DomainGroupName(i) & ",group")
		'msgbox "winnt://" & DomainName & "/" & DomainGroupName(i) & ",group"
		if err.number <> 0  then
			'msgbox err.description
		else
			'msgbox "added group"
		end if
	end if
next

for i = 1 to ubound(DomainUsername)								'Add the domain user(s)
	if DomainUsername(i) <> "" then	
		adgroup.add ("winnt://" & DomainName & "/" & DomainUsername(i) & ",user")
		'msgbox "winnt://" & DomainName & "/" & DomainUsername(i) & ",user"
		if err.number <> 0  then
			'msgbox err.description
		else
			'msgbox "added user"
		end if
	end if
next

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:it_techie
ID: 33465711
rbartczak: that works..plain & simple

rickardc:how would that look, with for example domain fabrikam.com and users:

lda24454
nsa45675
ndd36456

Does the extra code give me any added functionality

p.s. by the looks of your script I would still need to enter a computername?
0
 

Author Comment

by:it_techie
ID: 33465813
strComputer = "."
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)
strComputer = "."
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)

This crashes if a user is already a member of the administrators group, any way for the script to continue regardless?
0
 
LVL 1

Expert Comment

by:rickardc
ID: 33466149
This line gets the name of the computer you run it on.
Hostname = ws.ExpandEnvironmentStrings("%COMPUTERNAME%")

most of the "extra code" is for error checking.  This script also allows you to add more than one user to the group.  As it is, it will add domain users, and could also add contractors (which is commented out at the moment)
0
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 1000 total points
ID: 33466151
For the sake of completeness: I forgot to remove line 6 ("strComputer = "COMPUTERNAME"") when copying and pasting your script. And assuming you want to add two users, you actually don't need the second "Set ObjGroup=" line, either.
To continue with the script (once you've tested it) add
On Error Resume Next

On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
strComputer = objShell.ExpandEnvironmentStrings("%ComputerName%")
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)
Set objUser = GetObject("WinNT://domain/userid")
objGroup.Add(objUser.ADsPath)

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Group policies can be applied selectively to specific devices with the help of groups. Utilising this, it is possible to phase-in group policies, over a period of time, by randomly adding non-members user or computers at a set interval, to a group f…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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