Solved

VB Script for Adding New Local Admin Account

Posted on 2011-02-16
11
3,180 Views
Last Modified: 2012-05-11
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
Comment
Question by:Justin Durrant
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 34907088
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
 
LVL 23

Author Comment

by:Justin Durrant
ID: 34907146
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
 
LVL 28

Expert Comment

by:omgang
ID: 34907787
Where is strDomainOrWorkgroup assigned?
OM Gang
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:prashanthd
ID: 34908871
As suggested by omgang, can you assign a value strDomainOrWorkgroup and try.
0
 
LVL 23

Author Comment

by:Justin Durrant
ID: 34909535
I changed strDomainOrWorkgroup to strComputer. I get the same error.
0
 
LVL 23

Author Comment

by:Justin Durrant
ID: 34911188
Guys still with me?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 34911824
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
 
LVL 28

Assisted Solution

by:omgang
omgang earned 250 total points
ID: 34911834
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 34911859
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
 

Expert Comment

by:mikejbam
ID: 35338729
This does not appear to work with Windows 7. Would it be possible to update it to work with Windows 7?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35338764
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question