MkII Posted March 26, 2009 Posted March 26, 2009 I want to check if a user exists, if it does, reset the password, if it doesn't, create it as a local admin that doesn't expire & account doesn't expire. I've got separate parts working, but not all at once LOL Grab the computer name: ' Retrieve NetBIOS name of local computer.Set objNetwork =CreateObject("Wscript.Network")strComputer = objNetwork.ComputerName ' Bind to local computer object.Set objComputer = GetObject("WinNT://" &strComputer) Set Password & account to not expire: objUser.Put "UserFlags", 65600 ' Check user exists: Set objComputer = GetObject("WinNT://" & strComputer & "") objComputer.Filter = Array("user") intFound = 0 Or any recommendations of sites or utilities to learn this stuff. Thanks.
SYNACK Posted March 27, 2009 Posted March 27, 2009 The best place to look for this particular task would be here: Scripting Local Users using VBScript and here Scripting Local Groups using VBScript In combination with this (Geek Speak. > Local User and Group Management with VBScript and ADSI) that offers functions that do most of the things you want to do. 2
apeo Posted March 27, 2009 Posted March 27, 2009 Synacks post link to the Geek Speak function complation is well useful.. thanks. You could use it to do something like this: If Not userexists(username) Then createuser(username) addusertogroup(username,"Administrators") End If ChangePassword(username,password) 1
MkII Posted March 29, 2009 Author Posted March 29, 2009 (edited) Got this working, but it takes 6 seconds to run!!! Gotta be something wrong surely. Can anybody help? The delay occurs when the 'add to administrators group' section is added 'this section creates the new username if it doesn't exist QueryForUser("username") Set objNetwork = CreateObject("Wscript.Network") strComputer = objNetwork.ComputerName Set objComputer = GetObject("WinNT://" &strComputer) Set colAccounts = GetObject("WinNT://" & strComputer & "") Set objUser = colAccounts.Create("user", "username") objUser.SetPassword "password" objUser.Put "UserFlags", 65600 ' objUser.SetInfo 'add to administrators group Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group") Set objUser = GetObject("WinNT://" & strComputer & "/username,user") objGroup.Add(objUser.ADsPath) msgbox "username was created" 'this section just changes the password if the user exists Sub QueryForUser(strUserName) Set objlocal = GetObject("WinNT://.") objlocal.Filter = Array("user") For Each User In objlocal If lcase(User.Name) = lcase(strUserName) Then strComputer = "." Set objUser = GetObject("WinNT://" & strComputer & "/username, user") objUser.SetPassword "password" objUser.SetInfo msgbox User.Name & " already exists." & vbCrLf & "The password was re-set." WScript.Quit End If Next End Sub Edited March 29, 2009 by MkII
mesteele101 Posted August 23, 2011 Posted August 23, 2011 I hacked on the script a little. The first section adds the user to the Administrators group if the user doesn't exist. If the user exists it jumps to the next section, adds a password but doesn't check to see if they belong to the Administrators group, and if they don't it should add them. I'm not reak sure how to add this routine in? Did you find out why it taks so long to run? accUserName = "Operator" accUserPass = "p@ss" QueryForUser(accUserName) 'this section creates the new username if it doesn't exist Set objNetwork = CreateObject("Wscript.Network") strComputer = objNetwork.ComputerName Set objComputer = GetObject("WinNT://" &strComputer) Set colAccounts = GetObject("WinNT://" & strComputer & "") Set objUser = colAccounts.Create("user", accUserName) objUser.SetPassword accUserPass objUser.Put "UserFlags", 65600 ' objUser.SetInfo 'add user to administrators group Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group") Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ",user") objGroup.Add(objUser.ADsPath) msgbox "username was created" 'this section just changes the password if the user exists Sub QueryForUser(strUserName) Set objlocal = GetObject("WinNT://.") objlocal.Filter = Array("user") For Each User In objlocal If lcase(User.Name) = lcase(strUserName) Then strComputer = "." Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ", user") objUser.SetPassword accUserPass objUser.SetInfo msgbox User.Name & " already exists." & vbCrLf & "The password was re-set." WScript.Quit End If Next End Sub TIA..
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now