My script has a problem:
First section: Scans the users and adds a user if they don't exist Sets the password, sets the password to not expire, and assigns the user to the Administrators group.
Second section: If the user already exists it assigns a new password, and sets the password to not expire.
The problem is if the user exists it jumps to the second section. It doesn't check to see if the user belongs to the Administrators group. I can move the code from the first section that adds the user to the Administrators group and it works fine if the user does not already belong to that group. If the user already belongs to the Administrators group it throws an error.
What I'm looking for is help putting that routine in section two that checks if the user already belongs to the Administrators group and jumps over that routine.
Possibly someone might have a better idea. I have absolutely no training using Visual Basic. I can usually hack something together.
TIA...Code: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
There are currently 1 users browsing this thread. (0 members and 1 guests)