Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Check if user is builtin adminsitrator in vbscript

Avatar of justinBoucher
justinBoucherFlag for United States of America asked on
Scripting LanguagesVB Script
3 Comments1 Solution1085 ViewsLast Modified:
Recently we made an error in our images and we need to do some local account cleanup. I created a script that finds the local user called Bob (renamed, of course) and removes this user, then it renames the local admin account to Bob and resets the password. The problem is that in our test lab, we generated an error by renaming the admin account to Bob prior to running the script and and creating a regular user named admin. Which killed the script.

The script that I am supplying is a sub to a much larger script that pings every computer on our domain then runs the script, so I need to be able to identify this problem, and come up with a solution. I was thinking that the builtin adminstrator always has a 500 SID, but I have no clue how to check for that in vbscript. Thank you in advance for your assistance. Snippet provided below.
DIM objLocal, strComputer
DIM strPassword, strAdmin, strBob, logDate, logTime

strComputer = "."
logDate = FormatDateTime(Date(), 0)
logTime = Time

' The old name of the administrator user account (normally administrator)
strAdmin = "admin"
' The new name of the administrator user account
strBob = "bob"
' Password includes computername to have a unique password on all computers.
strPassword = "fakepassword" & strComputer

'Create Change Log file
ChangeOutputFile="C:\Changed.txt" 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set cFile = FSO.OpenTextFile(ChangeOutputFile, 8, True)

'Create Error Log File
ErrorOutputFile="C:\Errors.txt" 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set eFile = FSO.OpenTextFile(ErrorOutputFile, 8, True)



Set objLocal = GetObject("WinNT://" & strComputer & "")
objLocal.Filter = Array("User")

blnFound = FALSE

 For Each objUser in objLocal
 If objUser.Name = strAdmin Then
        blnFound = TRUE
        Exit For
    End If 
Next


If blnFound = TRUE Then

	Set objLocal = GetObject("WinNT://" & strComputer & "")
	objLocal.Filter = Array("User")

	blnFoundOld = FALSE

 	For Each objUser in objLocal
 	If objUser.Name = strBob Then
        	blnFoundOld = TRUE
        	Exit For
    	End If 
	Next


	If blnFoundOld = TRUE Then

		'Delete current garzaba user
		deleteUser strComputer,strBob

		'Log Details
		Wscript.Echo "Bob account removed on " & strComputer & ""
		cFile.WriteLine strComputer & " - Bob account removed >> " & logDate & " " & logTime

	Else
		Wscript.Echo "Bob account does not exist on " & strComputer & ""
		eFile.WriteLine strComputer & " - Bob account does not exist >> " & logDate & " " & logTime
	End If

	' Rename admin user account
	renameUser strComputer,strAdmin,strBob

	'Log Details
	Wscript.Echo "Admin account renamed to Bob on " & strComputer & ""
	cFile.WriteLine strComputer & " - Admin account renamed to Bob >> " & logDate & " " & logTime

	' Set password of admin user account
	setPWD strComputer,strBob,strPassword

	'Log details
	Wscript.Echo "Bob password changed on " & strComputer & ""
	cFile.WriteLine strComputer & " - Bob password changed >> " & logDate & " " & logTime
	
	WScript.Echo ""


Else
	Wscript.Echo "Admin account does not exist on " & strComputer & ""
	eFile.WriteLine strComputer & " - Admin account does not exist >> " & logDate & " " & logTime
	WScript.Echo ""
End If


cFile.Close
eFile.Close




' Reset password for a local user account on a given computer
sub setPWD(strComputer,strUser,strPassword)

	DIM objUser
	' Ignore error if user account isn't found or error changing password
	on error resume next 
	set objUser = getobject("WinNT://" & strComputer & "/" & strUser & ",user")
	if err.number = 0 then
		objUser.SetPassword strPassword
		objUser.SetInfo
	end if
	on error goto 0

end sub

' Rename a local user account on a given computer
sub renameUser(strComputer,strFromName, strToName)
	
	DIM objComputer,objUser
	' Ignore error if user account isn't found or error moving user
	on error resume next
	set objComputer = GetObject("WinNT://" & strComputer)
	set objUser = getobject("WinNT://" & strComputer & "/" & strFromName & ",user")
	if err.number = 0 then
		objComputer.MoveHere objUser.ADsPath,strToName
	end if
	on error goto 0
	
end sub

Sub deleteUser(strComputer,strUser)
	Dim objComputer
	Set objComputer = GetObject("WinNT://" & strComputer & "")
	if err.number = 0 then
		objComputer.Delete "user", strUser
	end if
	on error goto 0
End Sub
ASKER CERTIFIED SOLUTION
Avatar of justinBoucher
justinBoucherFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers