Link to home
Start Free TrialLog in
Avatar of Justin Durrant
Justin DurrantFlag for United States of America

asked on

VB Script for Adding New Local Admin Account

Hello, I am working on a script (see below) that will do the following:

1. Add a new local admin account to server
2. Setup password
3. Setup password not to expire
4. Add to local administrators group

The script executes without issue and does everything except set the password not to expire. Any idea what I might be missing? Thanks!

The script will be pushed to servers via GPO.


' specify account to create 
strAccount = "user" 
strPswd = "password" 


' get local computer name 
Set objNetwork = CreateObject("Wscript.Network") 
strComputer = objNetwork.ComputerName 


' check if local account already exists 
intExists = 0 
Set colAccounts = GetObject("WinNT://" & strComputer & "") 
colAccounts.Filter = Array("user") 
For Each objUser In colAccounts 
    If objUser.Name = strAccount Then 
     intExists = 1 
  End If 
Next 


If intExists = 0 Then 

   ' create local user 
   Set colAccounts = GetObject("WinNT://" & strComputer & "") 
   Set objUser = colAccounts.Create("user", strAccount) 


   ' set pswd 
   objUser.SetPassword strPswd 
   objUser.SetInfo 


   ' add to local admins group 
   Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group") 
   Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user") 
   objGroup.Add(objUser.ADsPath) 


   
   ' set password to not expire
   Set objUser = GetObject("WinNT://" & strDomainOrWorkgroup & "/" & _
   strComputer & "/" & strUser & ",User")
   objUserFlags = objUser.Get("UserFlags")
   objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
   objUser.Put "userFlags", objPasswordExpirationFlag 
   objUser.SetInfo

End If

Open in new window

Avatar of prashanthd
prashanthd
Flag of India image

Hi,

Please try the below code
Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000

' specify account to create 
strAccount = "user" 
strPswd = "password" 


' get local computer name 
Set objNetwork = CreateObject("Wscript.Network") 
strComputer = objNetwork.ComputerName 


' check if local account already exists 
intExists = 0 
Set colAccounts = GetObject("WinNT://" & strComputer & "") 
colAccounts.Filter = Array("user") 
For Each objUser In colAccounts 
    If objUser.Name = strAccount Then 
     intExists = 1 
  End If 
Next 


If intExists = 0 Then 

   ' create local user 
   Set colAccounts = GetObject("WinNT://" & strComputer & "") 
   Set objUser = colAccounts.Create("user", strAccount) 


   ' set pswd 
   objUser.SetPassword strPswd 
   objUser.SetInfo 


   ' add to local admins group 
   Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group") 
   Set objUser = GetObject("WinNT://" & strComputer & "/" & strAccount & ",user") 
   objGroup.Add(objUser.ADsPath) 
  
   ' set password to not expire
   Set objUser = GetObject("WinNT://" & strDomainOrWorkgroup & "/" & _
   strComputer & "/" & strUser & ",User")
   objUserFlags = objUser.Get("UserFlags")
   objPasswordExpirationFlag = ADS_UF_DONT_EXPIRE_PASSWD
   objUser.Put "userFlags", objPasswordExpirationFlag 
   objUser.SetInfo

End If

Open in new window

Avatar of Justin Durrant

ASKER

No luck...


C:\>cscript admin.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\admin.vbs(42, 4) (null): 0x80005000
Avatar of omgang
Where is strDomainOrWorkgroup assigned?
OM Gang
As suggested by omgang, can you assign a value strDomainOrWorkgroup and try.
I changed strDomainOrWorkgroup to strComputer. I get the same error.
Guys still with me?
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh yeah, I also removed that bit, since you already have an object bound to the new user, you don't need to do that again....
Avatar of mikejbam
mikejbam

This does not appear to work with Windows 7. Would it be possible to update it to work with Windows 7?
Hi, you will need to run a command prompt "As Administrator" and then run
cscript CreateUser.vbs

On Windows 7, it needs Admin rights....or disable UAC.

Regards,

Rob.