Solved

VB Script for Adding New Local Admin Account

Posted on 2011-02-16
11
3,235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

738 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