Jump to content

VB Script add local admin user if not exist


Recommended Posts

Posted

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:

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

objUser.Put "UserFlags", 65600 '

 

Check user exists:

Set objComputer = GetObject("WinNT://" & strComputer & "")
objComputer.Filter = Array("user")
intFound = 0

 

Or any recommendations of sites or utilities to learn this stuff. Thanks.

Posted

Synacks post link to the Geek Speak function complation is well useful.. thanks.

 

You could use it to do something like this:

 

If Not userexists(username) Then
createuser(username)
addusertogroup(username,"Administrators")
End If

ChangePassword(username,password)

  • Thanks 1
Posted (edited)

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

 

'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

Edited by MkII
  • 2 years later...
Posted

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?

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...