Error DataBase-One Place all Solutions Forums Blog Glossary    Contact Us
Search  
   
Browse by Category
Error DataBase-One Place all Solutions .: Operating Systems .: Windows Operating Systems .: Windows Scripts and Batch Files .: VBScript to Add user account or group to local computer group

VBScript to Add user account or group to local computer group

This script allows you to add user accounts to a (local or remote) workstation's local group such as the administrator, Remoe user group or power users group.

'This script allows you to add user accounts to a (local or remote)
' workstation's local group such as the administrator or power users group.
'
'What to do to set up the script:
'Figure out what userid's/groups you wish to add to the remote station.
'
'Example: testdomain\testID1
'
'Then figure out what local groups you wish to add these user accounts/groups
' to.
'
'Example: power users
'
'In this example, you would want to modify the Domainname to be
' "Testdomain", and modify the 'useraccount' variable to be
' "testID1".  Finally, you would want to modify the
' 'modifygroup' variable to be "Power Users".
'
'The nice thing about this script is that if you have a couple
' different domains, you can set your computers to be managed
' by the same accounts on both domains.  This is esecially
' handy if you are in the midst of an AD migration (as we were)
On error resume next

Dim DomainArray, DomainName
Dim UserArray, UserName, intdebug
Dim ModifyGroup, I, U, G, warn, strComputer

'*******************************************************************************
'Show some variables as they are being passed
intDebug = 0

'Show a message to the users if they are logging into a server if set to '0'
' They will see no warning.
warn = 0

'If you don't want this script installing anything on servers, set this to
' 'true' - set to 'false' if you don't care. :)
blnDontRunOnServers = true

'Set to true if you wish to prompt the person running the script to enter a
' remote computer name to add groups/users...
blnPrompt = false

'Comma delimited list of domains (the user account or group should be named
' EXACTLY the same on all domains listed).
DomainName = "yourdomain"

'Comma-delimited names of the user accounts/groups from the above domains that you
' wish to add.
UserAccount = "Support Group,Domain Admins"

'Comma-delimited names of the local groups you wish to add the above
' usernames to.
ModifyGroup = "Administrators,Administrateurs"

'*******************************************************************************

If blnPrompt = true Then
 strComputer = InputBox("Enter the name of the remote computer.","Enter computer name")
 If strComputer = "" Then wscript.quit
Else
 strComputer = "."
End If

On Error Resume Next

'If we want to look at the debugging info, then we should also look at the
' warnings shown to the users...
If Intdebug = 1 Then warn = 1

'split the comma-delimited domain list into an array
DomainArray = split(DomainName,",")

'split the user account domain list into an array
UserArray = split(UserAccount,",")

'split the group list into an array
GroupArray = split(ModifyGroup,",")

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery("Select Caption,ServicePackMajorVersion from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems

If err.number <> 0 then
  'If there is an error finding the Operating system version, then quit to err
  ' on the side of caution.
  wscript.quit
End If
    'If we've specified not to run on servers, then quit the script.
    If blnDontRunOnServers = true Then
      If InStr(objOperatingSystem.Caption, "Server") or InStr(objOperatingSystem.Caption, "Powered")  Then
      If warn = 1 Then MsgBox "This script is set to run on workstations only.  Please modify the '" _
        & ModifyGroup & "' group manually." & vbcrlf & vbcrlf & "Now quitting.",48,"Not for use on servers!"
        wscript.quit
      End If
    End If
Next

'Run through the groups that we listed above.
For G = 0 to UBound(GroupArray)

  'Attach to the group object using the WinNT provider.
 Set strgroup = GetObject("WinNT://"& strComputer &"/" & GroupArray(G))

 For I= 0 to UBound(DomainArray)
      If intdebug = 1 Then wscript.echo "Now processing domain: " & DomainArray(I)
      For U = 0 to UBound(UserArray)
         If intdebug = 1 Then wscript.echo "Now processing user: " & UserArray(U)
        
         If intdebug = 1 Then MsgBox "Adding " & DomainArray(I) & "\" & UserArray(U) & " to the local group " & ModifyGroup   
 
        'Add the domain/useraccount to the local group, defined as strGroup
        strGroup.Add "WinNT://"& DomainArray(I) &"/"& UserArray(U)
      Next
 Next
Next

'CheckError

Sub CheckError
  If Not err.number = 0 then
    Set ole = CreateObject("ole.err")
    If intdebug = 1 Then wscript.echo ole.oleError(err.Number)
     Err.clear
    Else
     'MsgBox "Done."
    End If
End sub


How helpful was this article to you?

Related Articles

article VB Script to Check if Local User account Exists
  Description: Checks to see if a...

(No rating)  3-24-2008    Views: 159   
article How can I change my user-account password from a remote computer?
You can change a Windows User Account...

(No rating)  3-11-2008    Views: 174   
article How to : Change the permissions a user or group has to a terminal connection
To change the permissions a user or group...

(No rating)  2-21-2008    Views: 154   

User Comments

Add Comment
No comments have been posted.