• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3433
  • Last Modified:

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

0
Justin Durrant
Asked:
Justin Durrant
  • 3
  • 3
  • 2
  • +2
2 Solutions
 
prashanthdCommented:
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

0
 
Justin DurrantAuthor Commented:
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
0
 
omgangCommented:
Where is strDomainOrWorkgroup assigned?
OM Gang
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
prashanthdCommented:
As suggested by omgang, can you assign a value strDomainOrWorkgroup and try.
0
 
Justin DurrantAuthor Commented:
I changed strDomainOrWorkgroup to strComputer. I get the same error.
0
 
Justin DurrantAuthor Commented:
Guys still with me?
0
 
RobSampsonCommented:
Hi, it looks like all you were missing was the definition for the Constant value for ADS_UF_DONT_EXPIRE_PASSWD, which I've put at the top.

Regards,

Rob.
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
	intUserFlags = objUser.Get("UserFlags")
	objUser.put "Userflags", intUserFlags Or ADS_UF_DONT_EXPIRE_PASSWD
	objUser.SetInfo

End If

Open in new window

0
 
omgangCommented:
Yes but confused.  Why did you change strDomainWorkgroup to strComputer?

Here's the statement from your code
   ' set password to not expire
   Set objUser = GetObject("WinNT://" & strDomainOrWorkgroup & "/" & _
   strComputer & "/" & strUser & ",User")

you're assigning the object to what appears to be a full path, e.g. WinNT://something/computername/username
so what is strDomainOrWorkgroup supposed to be?  You don't declare it nor assign a value to it in the procedure.
OM Gang
0
 
RobSampsonCommented:
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....
0
 
mikejbamCommented:
This does not appear to work with Windows 7. Would it be possible to update it to work with Windows 7?
0
 
RobSampsonCommented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now