Solved

Adding domain user to local administrators group via script¿

Posted on 2010-08-18
9
892 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
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
 
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

Join & Write a Comment

Suggested Solutions

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now