Solved

Adding domain user to local administrators group via script¿

Posted on 2010-08-18
9
905 Views
Last Modified: 2013-12-24
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
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
9 Comments
 
LVL 20

Expert Comment

by:woolnoir
ID: 33466219
How about ?

Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
If Not objUser.IsMember(objGroup.AdsPath) Then
' Add the domain group to the local group.
objGroup.Add(objUser.ADsPath)
End If
0
 
LVL 20

Expert Comment

by:woolnoir
ID: 33466236
I've modified the names and target to be added to local admins from a group to a user, but it should work. Youre just checking that the member isnt already in there, and if not, adding.
0
 
LVL 2

Expert Comment

by:hydrokid
ID: 33466271
This would definitely work. created a file and rename it adduser.vbs and put in all the code below.
Replace 667CA to which every user account you want.
if the domain is companyA then the user added to the local admin is companyA/667CA




Set oWshNet = CreateObject("WScript.Network" )

sUser = "667CA"

'sNetBIOSDomain = oWshNet.UserDomain
sComputer = oWshNet.ComputerName

Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators,group" )
Set oUser = GetObject("WinNT://AP/" & sUser & ",user" )
'Set oUser = GetObject("WinNT://" & sNetBIOSDomain & "/" & sUser & ",user" )

' suppress errors in case the user is already a member
On Error Resume Next
oGroup.Add(oUser.ADsPath)
On Error Goto 0
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.

 
LVL 1

Accepted Solution

by:
duraisamy earned 500 total points
ID: 33466577
Try this should work out.....

Dim DomainName
Dim UserAccount
Set net = WScript.CreateObject("WScript.Network")
local = net.ComputerName
DomainName = "DomainName"
UserAccount = "userAccount"

set group = GetObject("WinNT://"& local &"/Administrators")

on error resume next
group.Add "WinNT://"& DomainName &"/"& UserAccount &""
CheckError

sub CheckError
      if not err.number=0 then
      set ole = CreateObject("ole.err")
      MsgBox ole.oleError(err.Number), vbCritical
      err.clear
else
      MsgBox "Done."
end if
end sub

0
 
LVL 20

Expert Comment

by:woolnoir
ID: 33466587
And obviously the snipped i posted was designed to be augmented into your existing script.
0
 
LVL 20

Expert Comment

by:woolnoir
ID: 33466591
snippet*
0
 

Author Comment

by:it_techie
ID: 33471983
On Error Resume Next

this works

oh good morning btw
0
 
LVL 20

Expert Comment

by:woolnoir
ID: 33472173
Shouldnt points be awarded for this one, since myself and several others posted you a solution ?
0
 

Author Comment

by:it_techie
ID: 33472195
I've tried,

Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = GetObject("WinNT://domain/userid")
If Not objUser.IsMember(objGroup.AdsPath) Then
' Add the domain group to the local group.
objGroup.Add(objUser.ADsPath)
End If

Doesn't work

This one is a completely new script:

Dim DomainName
Dim UserAccount
Set net = WScript.CreateObject("WScript.Network")
local = net.ComputerName
DomainName = "DomainName"
UserAccount = "userAccount"

set group = GetObject("WinNT://"& local &"/Administrators")

on error resume next
group.Add "WinNT://"& DomainName &"/"& UserAccount &""
CheckError

sub CheckError
      if not err.number=0 then
      set ole = CreateObject("ole.err")
      MsgBox ole.oleError(err.Number), vbCritical
      err.clear
else
      MsgBox "Done."
end if
end sub


Although I do see the magic words on error resume next....


Ok, i'll give him the points
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

635 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