Add Domain user to Local Group via vb script

J

Jason

I am looking to add a domain user to the local admin group
via vb scripting. I have a script to add a local user to
the local group but am uncertain how to modify
appropriately to pull the domain user.

Any help?
 
T

Torgeir Bakken \(MVP\)

Jason said:
I am looking to add a domain user to the local admin group
via vb scripting. I have a script to add a local user to
the local group but am uncertain how to modify
appropriately to pull the domain user.

Any help?
Hi


'--------------------8<----------------------

Set oWshNet = CreateObject("WScript.Network")

sUser = "fill in some domain user name here"

sNetBIOSDomain = oWshNet.UserDomain
sComputer = oWshNet.ComputerName

Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators,group")
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
'--------------------8<----------------------


If the computers are in another domain than the user you
want to add, you will need to hard code the domain name
the user belongs to in the variable "sNetBIOSDomain".
 
G

Guest

To take this one step further, the DOMAIN USERS group had
been added to the Local Admin group. How can I, via
vbscript, remove that group from the workstations?

Thanks,
 
T

Torgeir Bakken \(MVP\)

To take this one step further, the DOMAIN USERS group had
been added to the Local Admin group. How can I, via
vbscript, remove that group from the workstations?
Hi


'--------------------8<----------------------
Set oWshNet = CreateObject("WScript.Network")

' computer name
sNode = oWshNet.ComputerName

' group name to remove user or group from
Set oGroup = GetObject("WinNT://" & sNode & "/Administrators")

' loop through all member of the group
For Each oUser In oGroup.Members

' get the name and make it lowercase
sGroupEntry = LCase(oUser.Name)

If (sGroupEntry = "domain users") Then
' remove entry from group
oGroup.Remove oUser.ADsPath
End if
Next
'--------------------8<----------------------
 
J

Jason

Thanks Much.

These scripts really help.
-----Original Message-----

Hi


'--------------------8<----------------------
Set oWshNet = CreateObject("WScript.Network")

' computer name
sNode = oWshNet.ComputerName

' group name to remove user or group from
Set oGroup = GetObject("WinNT://" & sNode & "/Administrators")

' loop through all member of the group
For Each oUser In oGroup.Members

' get the name and make it lowercase
sGroupEntry = LCase(oUser.Name)

If (sGroupEntry = "domain users") Then
' remove entry from group
oGroup.Remove oUser.ADsPath
End if
Next
'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
.
 
D

Delwin Lee [MSFT]

You can also do this pretty easily via the "net localgroup" command (though
this isn't really VBScript, you could call it from a vbs):

net localgroup Administrators /add DOMAIN\USERNAME
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top