Solved

VB Script for Adding New Local Admin Account

Posted on 2011-02-16
11
3,071 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
Comment Utility
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
Comment Utility
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
Comment Utility
Where is strDomainOrWorkgroup assigned?
OM Gang
0
 
LVL 12

Expert Comment

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

Author Comment

by:Justin Durrant
Comment Utility
I changed strDomainOrWorkgroup to strComputer. I get the same error.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Author Comment

by:Justin Durrant
Comment Utility
Guys still with me?
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now