Solved

Adding domain user to local administrators group via script¿

Posted on 2010-08-18
9
899 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
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 …

749 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