Ok...as my education continues into the world of scripting (still a
infant here), I have some questions.
I need to reset the password of all the local domain accounts on our
servers. I thought a script + GPO would be a handy method.

Since I am pretty new at scripting, I decided to google for some
suggestions and came up with a few interesting things and wanted to
ask some questions here.

First, I found this one from 'The Scripting guy':

Set objOU = GetObject("LDAP://OU=Finance, DC=fabrikam, DC=com")
objOU.Filter = Array("Computer")

For Each objItem in objOU
strComputer = objItem.CN
Set objUser = GetObject("WinNT://" & strComputer & "/
Administrator")
objUser.SetPassword("i5A2sj*!")
Next


You change the OU and Domain as you suggested. Which brings a quick
question. Suppose your OU has spaces. Something like: Server bin
Does that change how you set it up in the script? Do you need to quote
it within the quotes?

That script seems fairly straightforward.


Then I found this one:

'-------------------------------------------------------------------------------
' Initialization - Declare variables
'-------------------------------------------------------------------------------

Dim fsoIn, fsoOut
Dim inFile, outFile
Dim arrComputerNames
Dim objUser
Dim strComputer
Dim newPassword
Dim ErrorOccurred
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const inFilename = "servers.txt"
Const outFilename = "ChangePwdServers.log"

'-------------------------------------------------------------------------------
' Main script
'-------------------------------------------------------------------------------
On Error Resume Next
ErrorOccurred = False

' Insert WARNING here...
Msgbox ("WARNING: This script will change the local administrator
password for every " & _
"computer listed in SERVERS.TXT. If any services are running with
the local " & _
"administrator credentials, those services must be updated, or they
won't " & _
"start on the next boot. For this script to work, you must have
administrative " & _
"privileges on all of the remote computers you are changing the
password for.")

' Get new password
newPassword = Inputbox ("Please enter the new password.")

' Open the input file and skip the header line
Set fsoIn = CreateObject("scripting.filesystemobject")
Set inFile = fsoIn.OpenTextFile(inFilename, ForReading, True)
inFile.Skipline

' Open the log file (append mode) and timestamp the entry
Set fsoOut = CreateObject("scripting.filesystemobject")
Set outFile = fsoOut.OpenTextFile(outFilename, ForAppending, True)
outFile.writeline (Now & vbTab & "Starting script...")

While Not inFile.AtEndOfStream
arrComputerNames = Split(inFile.Readline, vbTab, -1, 1)
' arrComputerNames(0) contains the computer name
strComputer = arrComputerNames(0)

' Connect to the computer\administrator account
Set objUser = GetObject("WinNT://" & strComputer & "/Administrator,
user")
If Err.Number <> 0 Then
outFile.writeline Now & vbTab & "Error connecting to " & strComputer
& " --- " & Err.Description
Err.Clear
ErrorOccurred = True
Else
' Set the password for the account
objUser.SetPassword newPassword
objUser.SetInfo
If Err.Number <> 0 Then
outFile.writeline Now & vbTab & "Error setting password for " &
strComputer & _
"\Administrator" & " --- " & Err.Description
Err.Clear
ErrorOccurred = True
Else
outFile.writeline (Now & vbTab & "Password set for " & strComputer
& "\Administrator")
End If
End If
Wend

' Clean up the environment
outFile.writeline (Now & vbTab & "Ending script...")
inFile.close
outFile.close

If ErrorOccurred Then
msgbox "Script completed with errors. Please check the log file."
Else
MsgBox "Script completed successfully."
End If



Ok...not only is it VERY complex for me, but why the huge need for
code for something like this?
You look at the first one and it seems straightforward and easy. The
second, looks like a lot of extra, possibly unnecessary work.

I thought i'd start here.
Thanks,

Jas

Re: Script to change password for Local Admin account question by Richard

Richard
Thu May 10 23:06:29 CDT 2007

The two scripts are not that different. The first only deals with the
computers in one container (OU, container, domain). It has no error
trapping, so if anything goes wrong (a computer is not available), an error
is raised and the program halts.

The second script reads a list of computer names. This allows it to handle
computers in many containers, while skipping ones that should not be
altered, like Domain Controllers. It traps several possible errors and
handles them, such as if the computer is not available, or you lack
permission to change the password. This adds more lines of code, but makes
the program more robust. It even writes a log documenting what happened.
This program assumes a tab delimited text file of computer names. See
comments inline below:

"Jason W." <jasonwilliams74@gmail.com> wrote in message
news:1178837659.230832.144960@q75g2000hsh.googlegroups.com...
> Ok...as my education continues into the world of scripting (still a
> infant here), I have some questions.
> I need to reset the password of all the local domain accounts on our
> servers. I thought a script + GPO would be a handy method.
>
> Since I am pretty new at scripting, I decided to google for some
> suggestions and came up with a few interesting things and wanted to
> ask some questions here.
>
> First, I found this one from 'The Scripting guy':
>
> Set objOU = GetObject("LDAP://OU=Finance, DC=fabrikam, DC=com")
> objOU.Filter = Array("Computer")
>
> For Each objItem in objOU
> strComputer = objItem.CN
> Set objUser = GetObject("WinNT://" & strComputer & "/
> Administrator")
> objUser.SetPassword("i5A2sj*!")
> Next

The WinNT provider requires the NetBIOS name of the computer to bind to the
object. Usually, this is the same as the Common Name (the value of the cn
attribute), but this does not need to be the case. Technically, this program
should use the value of the sAMAccountName attribute, which is the NetBIOS
name of the computer with "$" appended on the end. The "$" would need to be
stripped off. I would use:

strComputer = objItem.sAMAccountName
strComputer = Left(strComputer, Len(strComputer) - 1)

>
>
> You change the OU and Domain as you suggested. Which brings a quick
> question. Suppose your OU has spaces. Something like: Server bin
> Does that change how you set it up in the script? Do you need to quote
> it within the quotes?

The OU name can have spaces and no extra quotes are required.

>
> That script seems fairly straightforward.
>
>
> Then I found this one:
>
> '-------------------------------------------------------------------------------
> ' Initialization - Declare variables
> '-------------------------------------------------------------------------------
>
> Dim fsoIn, fsoOut
> Dim inFile, outFile
> Dim arrComputerNames
> Dim objUser
> Dim strComputer
> Dim newPassword
> Dim ErrorOccurred
> Const ForReading = 1
> Const ForWriting = 2
> Const ForAppending = 8
> Const inFilename = "servers.txt"
> Const outFilename = "ChangePwdServers.log"
>
> '-------------------------------------------------------------------------------
> ' Main script
> '-------------------------------------------------------------------------------
> On Error Resume Next
> ErrorOccurred = False
>
> ' Insert WARNING here...
> Msgbox ("WARNING: This script will change the local administrator
> password for every " & _
> "computer listed in SERVERS.TXT. If any services are running with
> the local " & _
> "administrator credentials, those services must be updated, or they
> won't " & _
> "start on the next boot. For this script to work, you must have
> administrative " & _
> "privileges on all of the remote computers you are changing the
> password for.")
>
> ' Get new password
> newPassword = Inputbox ("Please enter the new password.")
>
> ' Open the input file and skip the header line
> Set fsoIn = CreateObject("scripting.filesystemobject")
> Set inFile = fsoIn.OpenTextFile(inFilename, ForReading, True)
> inFile.Skipline
>
> ' Open the log file (append mode) and timestamp the entry
> Set fsoOut = CreateObject("scripting.filesystemobject")
> Set outFile = fsoOut.OpenTextFile(outFilename, ForAppending, True)
> outFile.writeline (Now & vbTab & "Starting script...")

The fsoOut variable is not needed. The program could use fsoIn to open the
second file.

>
> While Not inFile.AtEndOfStream
> arrComputerNames = Split(inFile.Readline, vbTab, -1, 1)
> ' arrComputerNames(0) contains the computer name
> strComputer = arrComputerNames(0)
>
> ' Connect to the computer\administrator account
> Set objUser = GetObject("WinNT://" & strComputer & "/Administrator,
> user")
> If Err.Number <> 0 Then
> outFile.writeline Now & vbTab & "Error connecting to " & strComputer
> & " --- " & Err.Description
> Err.Clear
> ErrorOccurred = True
> Else
> ' Set the password for the account
> objUser.SetPassword newPassword
> objUser.SetInfo

The SetPassword method is immediate, so the SetInfo is not needed.

> If Err.Number <> 0 Then
> outFile.writeline Now & vbTab & "Error setting password for " &
> strComputer & _
> "\Administrator" & " --- " & Err.Description
> Err.Clear
> ErrorOccurred = True
> Else
> outFile.writeline (Now & vbTab & "Password set for " & strComputer
> & "\Administrator")
> End If
> End If
> Wend
>
> ' Clean up the environment
> outFile.writeline (Now & vbTab & "Ending script...")
> inFile.close
> outFile.close
>
> If ErrorOccurred Then
> msgbox "Script completed with errors. Please check the log file."
> Else
> MsgBox "Script completed successfully."
> End If
>
>
>
> Ok...not only is it VERY complex for me, but why the huge need for
> code for something like this?
> You look at the first one and it seems straightforward and easy. The
> second, looks like a lot of extra, possibly unnecessary work.
>
> I thought i'd start here.
> Thanks,
>
> Jas

I would code this differently, but the second example is more robust. It
handles errors, writes a log, and is more flexible. It allows you to specify
which computers to operate on. It even has comments.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: Script to change password for Local Admin account question by TimM

TimM
Tue May 15 12:11:02 CDT 2007

This is great, again you guys prove your the best. This post helped me on
many levels while I'm still learning vbscript. Thank you for taking the time
to help out the community.
--
Thanks
TimM


"Richard Mueller [MVP]" wrote:

> The two scripts are not that different. The first only deals with the
> computers in one container (OU, container, domain). It has no error
> trapping, so if anything goes wrong (a computer is not available), an error
> is raised and the program halts.
>
> The second script reads a list of computer names. This allows it to handle
> computers in many containers, while skipping ones that should not be
> altered, like Domain Controllers. It traps several possible errors and
> handles them, such as if the computer is not available, or you lack
> permission to change the password. This adds more lines of code, but makes
> the program more robust. It even writes a log documenting what happened.
> This program assumes a tab delimited text file of computer names. See
> comments inline below:
>
> "Jason W." <jasonwilliams74@gmail.com> wrote in message
> news:1178837659.230832.144960@q75g2000hsh.googlegroups.com...
> > Ok...as my education continues into the world of scripting (still a
> > infant here), I have some questions.
> > I need to reset the password of all the local domain accounts on our
> > servers. I thought a script + GPO would be a handy method.
> >
> > Since I am pretty new at scripting, I decided to google for some
> > suggestions and came up with a few interesting things and wanted to
> > ask some questions here.
> >
> > First, I found this one from 'The Scripting guy':
> >
> > Set objOU = GetObject("LDAP://OU=Finance, DC=fabrikam, DC=com")
> > objOU.Filter = Array("Computer")
> >
> > For Each objItem in objOU
> > strComputer = objItem.CN
> > Set objUser = GetObject("WinNT://" & strComputer & "/
> > Administrator")
> > objUser.SetPassword("i5A2sj*!")
> > Next
>
> The WinNT provider requires the NetBIOS name of the computer to bind to the
> object. Usually, this is the same as the Common Name (the value of the cn
> attribute), but this does not need to be the case. Technically, this program
> should use the value of the sAMAccountName attribute, which is the NetBIOS
> name of the computer with "$" appended on the end. The "$" would need to be
> stripped off. I would use:
>
> strComputer = objItem.sAMAccountName
> strComputer = Left(strComputer, Len(strComputer) - 1)
>
> >
> >
> > You change the OU and Domain as you suggested. Which brings a quick
> > question. Suppose your OU has spaces. Something like: Server bin
> > Does that change how you set it up in the script? Do you need to quote
> > it within the quotes?
>
> The OU name can have spaces and no extra quotes are required.
>
> >
> > That script seems fairly straightforward.
> >
> >
> > Then I found this one:
> >
> > '-------------------------------------------------------------------------------
> > ' Initialization - Declare variables
> > '-------------------------------------------------------------------------------
> >
> > Dim fsoIn, fsoOut
> > Dim inFile, outFile
> > Dim arrComputerNames
> > Dim objUser
> > Dim strComputer
> > Dim newPassword
> > Dim ErrorOccurred
> > Const ForReading = 1
> > Const ForWriting = 2
> > Const ForAppending = 8
> > Const inFilename = "servers.txt"
> > Const outFilename = "ChangePwdServers.log"
> >
> > '-------------------------------------------------------------------------------
> > ' Main script
> > '-------------------------------------------------------------------------------
> > On Error Resume Next
> > ErrorOccurred = False
> >
> > ' Insert WARNING here...
> > Msgbox ("WARNING: This script will change the local administrator
> > password for every " & _
> > "computer listed in SERVERS.TXT. If any services are running with
> > the local " & _
> > "administrator credentials, those services must be updated, or they
> > won't " & _
> > "start on the next boot. For this script to work, you must have
> > administrative " & _
> > "privileges on all of the remote computers you are changing the
> > password for.")
> >
> > ' Get new password
> > newPassword = Inputbox ("Please enter the new password.")
> >
> > ' Open the input file and skip the header line
> > Set fsoIn = CreateObject("scripting.filesystemobject")
> > Set inFile = fsoIn.OpenTextFile(inFilename, ForReading, True)
> > inFile.Skipline
> >
> > ' Open the log file (append mode) and timestamp the entry
> > Set fsoOut = CreateObject("scripting.filesystemobject")
> > Set outFile = fsoOut.OpenTextFile(outFilename, ForAppending, True)
> > outFile.writeline (Now & vbTab & "Starting script...")
>
> The fsoOut variable is not needed. The program could use fsoIn to open the
> second file.
>
> >
> > While Not inFile.AtEndOfStream
> > arrComputerNames = Split(inFile.Readline, vbTab, -1, 1)
> > ' arrComputerNames(0) contains the computer name
> > strComputer = arrComputerNames(0)
> >
> > ' Connect to the computer\administrator account
> > Set objUser = GetObject("WinNT://" & strComputer & "/Administrator,
> > user")
> > If Err.Number <> 0 Then
> > outFile.writeline Now & vbTab & "Error connecting to " & strComputer
> > & " --- " & Err.Description
> > Err.Clear
> > ErrorOccurred = True
> > Else
> > ' Set the password for the account
> > objUser.SetPassword newPassword
> > objUser.SetInfo
>
> The SetPassword method is immediate, so the SetInfo is not needed.
>
> > If Err.Number <> 0 Then
> > outFile.writeline Now & vbTab & "Error setting password for " &
> > strComputer & _
> > "\Administrator" & " --- " & Err.Description
> > Err.Clear
> > ErrorOccurred = True
> > Else
> > outFile.writeline (Now & vbTab & "Password set for " & strComputer
> > & "\Administrator")
> > End If
> > End If
> > Wend
> >
> > ' Clean up the environment
> > outFile.writeline (Now & vbTab & "Ending script...")
> > inFile.close
> > outFile.close
> >
> > If ErrorOccurred Then
> > msgbox "Script completed with errors. Please check the log file."
> > Else
> > MsgBox "Script completed successfully."
> > End If
> >
> >
> >
> > Ok...not only is it VERY complex for me, but why the huge need for
> > code for something like this?
> > You look at the first one and it seems straightforward and easy. The
> > second, looks like a lot of extra, possibly unnecessary work.
> >
> > I thought i'd start here.
> > Thanks,
> >
> > Jas
>
> I would code this differently, but the second example is more robust. It
> handles errors, writes a log, and is more flexible. It allows you to specify
> which computers to operate on. It even has comments.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>