Re: script to add a user to the local administrators group

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 01/14/05


Date: Fri, 14 Jan 2005 20:01:27 +0100

emebohw2@netscape.net wrote:

> Can someone post a (complete!) vbscript (wmi, wsh, whatever!) that will
> add a single user to the local administrator group? I have a BAT file
> that does this, but it hangs sometimes for no good reason so I would
> like to try vb.
>
Hi

Adding a local user account to the Administrators group:

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

' name of user to be added
sUser = "tst"

' name of the group the user is to be added to
sGroupname = "Administrators"

' get computer name
Set oWshNet = CreateObject("WScript.Network")
sComputerName = oWshNet.ComputerName

' connect to the group
Set oGroup = GetObject("WinNT://" & sComputerName & "/" & sGroupname)

' connect to the user
Set oUser = GetObject("WinNT://" & sComputerName & "/" & sUser & ",user")

' Add the user to the group
' Use error handling in case the account is a member already
On Error Resume Next
oGroup.Add(oUser.ADsPath)
On Error Goto 0
'--------------------8<----------------------

Adding a domain user account to the Administrators group:

'--------------------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".

-- 
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


Relevant Pages

  • Limit domain login to Administrator Group
    ... winxp computers as part of the domain. ... each user is a part of their local administrator group on ... localuser2 can not login ...
    (microsoft.public.win2000.group_policy)
  • Re: administrator rights for computer
    ... You can not add a user to the domain admin group from their computer. ... the user logging on with their domain account and being administrator of the ... >> Add their domain account to the local administrator group of their ...
    (microsoft.public.win2000.security)
  • Listing Users that are Part of the Local Administrator Group
    ... I'm trying to find/write a script that can return the members of the Local ... Administrator group on Win 2K/XP machines. ... lists the computers in my domain with the Domain users that belong to those ... can't figure out how to pull the users from the local administrator group. ...
    (microsoft.public.scripting.vbscript)
  • Re: Listing Users that are Part of the Local Administrator Group
    ... I have an active directory 2003 domain environment and basically want to be able to generate a text file that lists the computers in my domain with the Domain users that belong to those computers' local Administrator group. ... Very new to Windows scripting, and I can't figure out how to pull the users from the local administrator group. ... fOutFile.WriteLine vbCrlF & "Other accounts:" fOutFile.WriteLine sOthers fOutFile.Close ...
    (microsoft.public.scripting.vbscript)
  • Re: Add global security group to local administrators group
    ... security group to my local administrator group on my Windows 2000 / XP machines, any ideas, as always thanks in advance.. ... you will need to hard code the domain name the group belongs to in the ... torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.scripting.vbscript)

Loading