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
Here's the last server it changed, then the extra entries, there are quite a few.
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 :
========================================================================