+ Post New Thread
Results 1 to 5 of 5
Scripts Thread, VB Script add local admin user if not exist in Coding and Web Development; I want to check if a user exists, if it does, reset the password, if it doesn't, create it as ...
  1. #1
    mark's Avatar
    Join Date
    Jun 2005
    Posts
    3,983
    Thank Post
    268
    Thanked 52 Times in 46 Posts
    Blog Entries
    2
    Rep Power
    47

    VB Script add local admin user if not exist

    I want to check if a user exists, if it does, reset the password, if it doesn't, create it as a local admin that doesn't expire & account doesn't expire.

    I've got separate parts working, but not all at once LOL

    Grab the computer name:
    Code:
    ' Retrieve NetBIOS name of local computer.Set objNetwork =CreateObject("Wscript.Network")strComputer = objNetwork.ComputerName
    
    ' Bind to local computer object.Set objComputer = GetObject("WinNT://" &strComputer)
    Set Password & account to not expire:
    Code:
    objUser.Put "UserFlags", 65600 '
    Check user exists:
    Code:
    Set objComputer = GetObject("WinNT://" & strComputer & "")
    objComputer.Filter = Array("user")
    intFound = 0
    Or any recommendations of sites or utilities to learn this stuff. Thanks.

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,239
    Thank Post
    882
    Thanked 2,742 Times in 2,316 Posts
    Blog Entries
    11
    Rep Power
    784
    The best place to look for this particular task would be here: Scripting Local Users using VBScript and here Scripting Local Groups using VBScript

    In combination with this (Geek Speak. > Local User and Group Management with VBScript and ADSI) that offers functions that do most of the things you want to do.

  3. 2 Thanks to SYNACK:

    apeo (27th March 2009), mark (27th March 2009)

  4. #3
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42
    Synacks post link to the Geek Speak function complation is well useful.. thanks.

    You could use it to do something like this:

    Code:
    If Not userexists(username) Then
    	createuser(username)
    	addusertogroup(username,"Administrators")
    End If
    
    ChangePassword(username,password)

  5. Thanks to apeo from:

    mark (27th March 2009)

  6. #4
    mark's Avatar
    Join Date
    Jun 2005
    Posts
    3,983
    Thank Post
    268
    Thanked 52 Times in 46 Posts
    Blog Entries
    2
    Rep Power
    47
    Got this working, but it takes 6 seconds to run!!! Gotta be something wrong surely. Can anybody help?

    The delay occurs when the 'add to administrators group' section is added

    Code:
    'this section creates the new username if it doesn't exist
    QueryForUser("username")
    
                    Set objNetwork = CreateObject("Wscript.Network")
                    strComputer = objNetwork.ComputerName
                    Set objComputer = GetObject("WinNT://" &strComputer)
    
                    Set colAccounts = GetObject("WinNT://" & strComputer & "")
                    Set objUser = colAccounts.Create("user", "username")
                    objUser.SetPassword "password"
                    objUser.Put "UserFlags", 65600 '
                    objUser.SetInfo
    
    'add to administrators group
                    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
                    Set objUser = GetObject("WinNT://" & strComputer & "/username,user")
                    objGroup.Add(objUser.ADsPath)
    
               msgbox "username was created"
    
    'this section just changes the password if the user exists
     Sub QueryForUser(strUserName)
        Set objlocal = GetObject("WinNT://.")
        objlocal.Filter = Array("user")
        For Each User In objlocal
            If lcase(User.Name) = lcase(strUserName) Then
    
                    strComputer = "."
                    Set objUser = GetObject("WinNT://" & strComputer & "/username, user")
                    objUser.SetPassword "password"
                    objUser.SetInfo
    
                msgbox User.Name & " already exists." & vbCrLf & "The password was re-set."
                WScript.Quit
            End If    
        Next
     End Sub
    Last edited by mark; 29th March 2009 at 10:23 PM.

  7. #5

    Join Date
    Jul 2011
    Location
    USA
    Posts
    27
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I hacked on the script a little. The first section adds the user to the Administrators group if the user doesn't exist. If the user exists it jumps to the next section, adds a password but doesn't check to see if they belong to the Administrators group, and if they don't it should add them.

    I'm not reak sure how to add this routine in?

    Did you find out why it taks so long to run?

    Code:
    accUserName = "Operator"
    accUserPass = "p@ss"
    
    			QueryForUser(accUserName)
    				'this section creates the new username if it doesn't exist
                    Set objNetwork = CreateObject("Wscript.Network")
                    strComputer = objNetwork.ComputerName
                    Set objComputer = GetObject("WinNT://" &strComputer)
    
                    Set colAccounts = GetObject("WinNT://" & strComputer & "")
                    Set objUser = colAccounts.Create("user", accUserName)
                    objUser.SetPassword accUserPass
                    objUser.Put "UserFlags", 65600 '
                    objUser.SetInfo
    
    				'add user to administrators group
                    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
                    Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ",user")
                    objGroup.Add(objUser.ADsPath)
    
               msgbox "username was created"
    
    				'this section just changes the password if the user exists
    				Sub QueryForUser(strUserName)
    					Set objlocal = GetObject("WinNT://.")
    					objlocal.Filter = Array("user")
    					For Each User In objlocal
    					If lcase(User.Name) = lcase(strUserName) Then
    						strComputer = "."
    						Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ", user")
    						objUser.SetPassword accUserPass
    						objUser.SetInfo
    						msgbox User.Name & " already exists." & vbCrLf & "The password was re-set."
    						WScript.Quit
    					End If    
    			Next
    					End Sub
    TIA..

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 7
    Last Post: 26th February 2010, 10:00 AM
  2. add user as local admin
    By TechSupp in forum Scripts
    Replies: 2
    Last Post: 22nd September 2008, 11:54 AM
  3. Replies: 5
    Last Post: 14th June 2008, 12:18 AM
  4. Add an IP address to local host
    By FN-GM in forum Windows
    Replies: 12
    Last Post: 22nd January 2008, 08:29 PM
  5. Script to create a local user on a XP box.
    By tosca925 in forum Scripts
    Replies: 3
    Last Post: 12th October 2005, 12:04 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •