Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding domain user to local administrators group via script¿

Posted on 2010-08-18
9
Medium Priority
?
911 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 1

Accepted Solution

by:
duraisamy earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

971 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