Need help with VBS to check user account for Administrator group and bypass
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.
accUserName = "Operator"
accUserPass = "p@ss"
'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.Put "UserFlags", 65600 '
'add user to administrators group
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ",user")
msgbox "username was created"
'this section just changes the password if the user exists
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")
msgbox User.Name & " already exists." & vbCrLf & "The password was re-set."