Jump to content

VBScript to add user as an administrator


Recommended Posts

Hi,

I need to write a script that will allow me to add a user as an administrator to a computer the first time they ever try to log on to it.

All the computers are part of a small network that uses AD to administrate it.

Basically I need the logon script to...

Read the Username and Password input by the user.

Check to see if the user is in the AD

Add the user to the Computer within the Administrator group

I'm not really sure how to go about this though and I think there will be credential problems as the new user will not have authority to change it's group to the administrator group.

Steve

Link to comment
Share on other sites


You will have to add the computer name on the network

This has no checks to see if the computer is active on the network.

But it will add some one to the Administators Groups.

Save As AddToAdmin.vbs

Dim A1, A2, Var1, Network, strComputer, ZZ1 
Var1 = Space(3) & Chr(62)
Set Network = CreateObject("WScript.Network")
'/-> Example : Change To strComputer = "Place Computer Name Here"
strComputer = Network.ComputerName
'/-> Inputbox To GetThe User Name
GetTheName()
Function GetTheName()
A1 = InputBox(Var1 & Ucase(" Add To The Administrators Group") & vbCrLf &_
"Type the User Name you would like to be added to the" &_
" Administrators Group." & vbCrLf & "Type Quit or quit to exit this," &_
" and not add a User to the Administrators Group.","Add User",,5500,4800)
If InStr(A1,"Quit") Or InStr(A1,"quit") Then : WScript.Quit() : End If
If A1 = "" Then MissingName() End If
If A1 <> "" Then AddName() End If
End Function
'/-> If There No Text In The Inputbox
Function MissingName()
ZZ1 = MsgBox("There was no name type in the inputbox" & vbCrLf &_
"There has to be a name to add to the, " & vbCrLf &_
"administator group. Press yes to re run" & vbCrLf &_
"the add name or press no to exit and quit", 4 + 32 + 4096, "No User Input")
If ZZ1 = 6 Then AddName() End If
If ZZ1 = 7 Then MsgBox "User Cancel the add name", 0 + 32 + 4096,"Quit" End If
End Function
'/-> Add The User Name
Function AddName()
'/-> Confirms The User Name
ZZ1 = MsgBox("Is this the correct name " & A1 & vbCrLf &_
"Yes to continue, No to Re Do the name", 4 + 32 + 4096, "Confirm")
If ZZ1 = 6 Then
Set colAccounts = GetObject("WinNT://" & strComputer & "")
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = colAccounts.Create("user", "" & A1 & "")
objUser.SetInfo
objGroup.Add(objUser.ADsPath)
'/-> Popup When Scripts Finished Adding User Closes It Self In 4 Seconds
CreateObject("Wscript.Shell").Popup _
"Completed Adding To Administrators Group",4,"Completed", 0 + 32 + 4096
End If
If ZZ1 = 7 Then GetTheName() End If
End Function

This has no network check but I added a password box.

1:\ Checks to see if there a password

2:\ Checks Length of password

The script will not do anything until a Six digit password has been filled in.

3:\ This will quit if after 3 times there is no password detected.

Save As AddAdminAndPassword.vbs

Dim A1, A2, A3, Network, strComputer, Var1, ZZ1 
Var1 = Space(3) & Chr(62)
Set Network = CreateObject("WScript.Network")
strComputer = Network.ComputerName
'/-> Inputbox To GetThe User Name
GetTheName()
Function GetTheName()
A1 = InputBox(Var1 & Ucase(" Add To The Administrators Group") & vbCrLf &_
"Type the User Name you would like to be added to the" &_
" Administrators Group." & vbCrLf & "Type Quit or quit to exit this," &_
" and not add a User to the Administrators Group.","Add User",,5500,4800)
'/-> Check For Quit Or quit in the Inputbox
If InStr(A1,"Quit") Or InStr(A1,"quit") Then : WScript.Quit() : End If
'/-> Check To See If Inputbox Is Empty
If A1 = "" Then MissingName() End If
'/-> Check To See If The Inputbox Is Not Empty
If A1 <> "" Then AddPassword() End If
End Function
'/-> Add The Password
Function AddPassword()
A2 = InputBox(Space(3) & Chr(62) &_
Ucase(" Must Be Filled In Or Script Will Exit") & vbCrLf & _
"Type in a no less then Six Digit Letters or Number combination" &_
" for the user password","Add Password",,5500,4800)
'/-> Check For A Value From The Inputbox
If A2 = "" Then :A3 = A3 + 1 : PassWordError() : End If
If A2 <> "" Then : PassWordCheck() : End If
End Function
'/-> If There No Text In The Inputbox
Function MissingName()
ZZ1 = MsgBox("There was no name type in the inputbox" & vbCrLf &_
"There has to be a name to add to the, " & vbCrLf &_
"administator group. Press yes to re run" & vbCrLf &_
"the add name or press no to exit and quit", 4 + 32 + 4096, "No User Input")
If ZZ1 = 6 Then AddName() End If
If ZZ1 = 7 Then MsgBox "User Cancel the add name", 0 + 32 + 4096,"Quit" End If
End Function
'/-> Add The User Name And Password
Function AddName()
'/-> Confirms The User Name
ZZ1 = MsgBox("Is this the correct name " & A1 & vbCrLf &_
"Yes to continue, No to Re Do the name", 4 + 32 + 4096, "Confirm")
If ZZ1 = 6 Then
Set colAccounts = GetObject("WinNT://" & strComputer & "")
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
Set objUser = colAccounts.Create("user", "" & A1 & "")
objUser.SetPassword "" & A2 & ""
objUser.SetInfo
objGroup.Add(objUser.ADsPath)
'/-> Popup When Scripts Finished Adding User Closes It Self In 4 Seconds
CreateObject("Wscript.Shell").Popup _
"Completed Adding To Administrators Group",4,"Completed", 0 + 32 + 4096
End If
If ZZ1 = 7 Then GetTheName() End If
End Function
'/-> Check Lengh Of PassWord
Function PassWordCheck()
If Len(A2) < 6 Then
MsgBox "This Password is not Six characters long" & vbCrLf &_
"Please make sure the Password is at least" & vbCrLf &_
"Six characters of either Letters or Numbers", 0 + 32 + 4096, "Error Less the Six Characters"
AddPassword()
Else
AddName()
End If
End Function
'/-> If No Password Is Detected
Function PassWordError()
If A3 = 3 Then
MsgBox Var1 & Ucase("Error Exceeded Password Retries") & vbCrLf &_
"This is the third time that No Password been detected." & vbCrLf &_
"This script will now exit as a Password is required." & vbCrLf &_
"Contact your Network Or System Administator for more" & vbCrLf &_
"information on this Error.", 0 + 32 + 4096,"Exceeded Password Retries! " & A3
WScript.Quit
End If
If A3 = 2 Then
MsgBox Var1 & Ucase("Error There Was No Password") & vbCrLf &_
"This script requires a Password when adding a user" & vbCrLf &_
"to the Administrators Group" & vbCrLf &_
"This is the second time that this Error has occurred" & vbCrLf &_
"This is your last chance to add the Password!", 0 + 32 + 4096,"No Password Detected Error " & A3
AddPassword()
End If
If A3 = 1 Then
MsgBox Var1 & Ucase("Error There Was No Password") & vbCrLf &_
"This script requires a Password when adding a user" & vbCrLf &_
"to the Administrators Group", 0 + 32 + 4096,"No Password Detected Error " & A3
AddPassword()
End If
End Function

Or you could look at this HTA I made and change it to suit your needs.

HTA Add User Thread

Link To DL SFX HTA

Edited by gunsmokingman
Link to comment
Share on other sites

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...