Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Local admin password change script

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi all
I've got a script that changes the local admin password on all of our servers. It works with the exception that after it does the last server, it prints a bunch of blank entries. I know this has got to be in the for each section but I can't figure it out. Here's the script

Code:
'==========================================================================
' 
' NAME:		Local Admin Password Change.vbs	
' 
' AUTHOR:	Gene Magerr
' EMAIL:	genemagerr@hotmail.com
'
' COMMENT:	This script will change the local administrators password
'			on all of the computers in the c:\servers.txt file.
'
' VERSION HISTORY:
' 1.0   01/17/2008  Initial release
' 1.1   01/24/2008	Did some work on the formatting in email.
'
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO, TestMode, strPassword, objTextFile
Dim strComputers, arrComputer, strComputer, objUser, arrComputers
Dim objMessages, objMessage, objEmail, strMessage

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
strPassword = InputBox("Please enter a new password:", "Local administrators password change.")
 'Check that user entered a password
If strPassword = "" Then
	WScript.Quit
End If

If Not objFSO.FileExists("c:\messages.txt") Then
objFSO.CreateTextFile("c:\messages.txt")
End If

Set objMessages = objFSO.OpenTextFile("c:\messages.txt", 2)
Set objTextFile = objFSO.OpenTextFile("c:\servers.txt", 1)
objMessages.WriteLine(Now & vbTab & "Starting script..." & vbCrLf) 

strComputers = objTextFile.ReadAll
objTextFile.Close

arrComputers = Split(strComputers, vbCrLf)
    
 'Enumerate each server in the text file
For Each strComputer In arrComputers

	 'Connect to Administrator acccount on server using WinNT provider
	Set objUser = GetObject("WinNT://" & strComputer & "/Administrator,user")
	 'Check if we connected to the user object successfully

	If Err.Number <> 0 Then
	On Error Goto 0
		 'Display an error message & clear the error
		objMessages.WriteLine Now & vbTab & "Unable to connect to Administrator user object on server " & strComputer
		objMessages.WriteLine "Error #" & Err.Number
		objMessages.WriteLine "Error Message : " & Err.Description
		objMessages.WriteLine "========================================================================"
		Err.Clear
	Else	
	
 'Change the password
		objUser.SetPassword strPassword
		objUser.SetInfo ' Save Changes

	If Err.Number <> 0 Then
	On Error Goto 0
		'Display an error message & clear the error
		objMessages.WriteLine Now & vbTab & "Unable to change the Administrator password on server " & strComputer
		objMessages.WriteLine "Error #" & Err.Number
		objMessages.WriteLine "Error Message : " & Err.Description
		objMessages.WriteLine "========================================================================"
		Err.Clear
	
	Else 
		objMessages.WriteLine Now & vbTab & "Password successfully changed on: " & strComputer
		objMessages.WriteLine "========================================================================"
	End If
  End If
Next

objMessages.WriteLine vbCrLf & Now & vbTab & "Ending script..." 
objTextFile.Close
objMessages.Close

Set objMessages = objFSO.OpenTextFile("c:\messages.txt", 1)
strMessage = objMessages.ReadAll
objMessages.Close

'WScript.Echo strMessage

Set objEmail = CreateObject("CDO.Message")
objEmail.Sender = "sysadmins@company.com"
objEmail.To = "gmagerr@company.com"
objEmail.Subject = "Local Administrators Password Change Results"

'objEmail.TextBody = objEmail.TextBody & "This is the password I was prompted for. " & strPassword & vbCrLf & vbCrLf
objEmail.TextBody = objEmail.TextBody & strMessage
objEmail.TextBody = objEmail.TextBody & "Script ran on " & Date()

objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "mail.company.com"
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update

objEmail.Send
Set objEmail = Nothing

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================

Here's the last server it changed, then the extra entries, there are quite a few.

Code:
========================================================================
1/31/2008 3:08:24 PM	Password successfully changed on: WDCACS1
========================================================================
1/31/2008 3:08:24 PM	Unable to connect to Administrator user object on server 
Error #-2147463168
Error Message : 
========================================================================
1/31/2008 3:08:24 PM	Unable to connect to Administrator user object on server 
Error #-2147463168
Error Message : 
========================================================================
1/31/2008 3:08:24 PM	Unable to connect to Administrator user object on server 
Error #-2147463168
Error Message : 
========================================================================
1/31/2008 3:08:24 PM	Unable to connect to Administrator user object on server 
Error #-2147463168
Error Message : 
========================================================================
 
You most likely have blank lines at the end of your file.

I would check the length.

Code:
For Each strComputer In arrComputers
   [red]If Len(strComputer) > 0 Then
        [blue]your worker code here[/blue]
   End If[/red]

I have a similar script in my FAQ you might like to check out: faq329-6378



I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Marc,
I thought about blank lines last night. I will check it out. One thing i did notice after running the script was a couple of the servers had their Administrator account changes. Wonder if there's a was to put the Admin SID into a variable?

Thanks again.
 
This seemed to work out ok.
Code:
'==========================================================================
' 
' NAME:		Local Admin Password Change.vbs	
' 
' AUTHOR:	Gene Magerr
' EMAIL:	genemagerr@hotmail.com
'
' COMMENT:	This script will change the local administrators password
'			on all of the computers in the c:\servers.txt file.
'
' VERSION HISTORY:
' 1.0   01/17/2008  Initial release
' 1.1   01/24/2008	Did some work on the formatting in email.
'
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO, TestMode, strPassword, objTextFile
Dim strComputers, arrComputer, strComputer, objUser, arrComputers
Dim objMessages, objMessage, objEmail, strMessage, AdminName

Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
strPassword = InputBox("Please enter a new password:", "Local administrators password change.")
 'Check that user entered a password
If strPassword = "" Then
	WScript.Quit
End If

If Not objFSO.FileExists("c:\messages.txt") Then
objFSO.CreateTextFile("c:\messages.txt")
End If

Set objMessages = objFSO.OpenTextFile("c:\messages.txt", 2)
Set objTextFile = objFSO.OpenTextFile("c:\servers.txt", 1)
objMessages.WriteLine(Now & vbTab & "Starting script..." & vbCrLf) 

strComputers = objTextFile.ReadAll
objTextFile.Close

arrComputers = Split(strComputers, vbCrLf)
    
 'Enumerate each server in the text file
For Each strComputer In arrComputers

If Len(strComputer) > 0 Then

On Error Resume Next
Set objUsers = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2").ExecQuery( _
"Select Name, SID from Win32_UserAccount WHERE Domain = '" & strComputer & "'")

For Each objUser In objUsers
	If Left(objUser.SID, 9) = "S-1-5-21-" And Right(objUser.SID, 4) = "-500" Then
AdminName = objUser.Name

Exit For
	End If
Next

'Connect to Administrator acccount on server using WinNT provider
	
	Set objUser = GetObject("WinNT://" & strComputer & "/" & AdminName & ",user")
	 
	 'Check if we connected to the user object successfully
	If Err.Number <> 0 Then
	On Error Goto 0
		 'Display an error message & clear the error
		objMessages.WriteLine Now & vbTab & "Unable to connect to Administrator user object on server " & strComputer
		objMessages.WriteLine "Error #" & Err.Number
		objMessages.WriteLine "Error Message : " & Err.Description
		objMessages.WriteLine "========================================================================"
		Err.Clear
	Else	
	
 'Change the password
		objUser.SetPassword strPassword
		objUser.SetInfo ' Save Changes

	If Err.Number <> 0 Then
	On Error Goto 0
		'Display an error message & clear the error
		objMessages.WriteLine Now & vbTab & "Unable to change the Administrator password on server " & strComputer
		objMessages.WriteLine "Error #" & Err.Number
		objMessages.WriteLine "Error Message : " & Err.Description
		objMessages.WriteLine "========================================================================"
		Err.Clear
	
	Else 
		objMessages.WriteLine Now & vbTab & "Password successfully changed on: " & strComputer
		objMessages.WriteLine "========================================================================"
	End If
  End If
End If
Next

objMessages.WriteLine vbCrLf & Now & vbTab & "Ending script..." 
objMessages.Close

Set objMessages = objFSO.OpenTextFile("c:\messages.txt", 1)
strMessage = objMessages.ReadAll
objMessages.Close

'WScript.Echo strMessage

Set objEmail = CreateObject("CDO.Message")
objEmail.Sender = "sysadmins@rand.org"
objEmail.To = "gmagerr@rand.org"
objEmail.Subject = "Local Administrators Password Change Results"

'objEmail.TextBody = objEmail.TextBody & "This is the password I was prompted for. " & strPassword & vbCrLf & vbCrLf
objEmail.TextBody = objEmail.TextBody & strMessage
objEmail.TextBody = objEmail.TextBody & "Script ran on " & Date()

objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "mail.rand.org"
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
'objEmail.CC = "engay@rand.org" 

objEmail.Send
Set objEmail = Nothing

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top