Quote:
Originally Posted by astorrs It would be much better to use the script linked to by tiersten. Even better would be a script that generated a random password for each computer and wrote the output to a CSV file. |
Technically better for security, but may cause difficulties to manage the computers. Setting them all to the same is easier to manage.
Here's a PowerShell script I made to do it, which includes a CSV report of which ones failed and succeeded to have their password changed. The error handling isn't the best way of doing it, but I never had a chance to go back and improve it.
#Sets a variable for today's date. This will be used below to generate a uniquely named report each time the script is run.
#Ex: Friday, March 27, 2009 2:09:40 PM = 03272009-140340
$date = get-date -uformat "%m%d%Y-%H%m%S"
#Sets the report's file name consisting of (failedpwdchanges + $date.csv)
$filename = "pwdchanges" + $date + ".csv"
#Sets the target local account on the remote machine for the password change.
$account="administrator"
#Sets the new password
$password="Passwordhere"
#sets the servers which the account password will be changed.
$computers = get-content C:\temp\computers.txt
#Clears the special $error variable. This is to ensure that only failed target computers will be captured to build the failure list.
$error.clear()
#On each computer in the $computers collection, set $account's password to $password, and write the change to the account.
foreach ($computer in $computers) {
Write-Host "Changing $account password on $computer"
#Since $computers is an array, and a CSV report will be created, this puts the computer name into a Name property of the object that can be included in the report.
$computer | Add-Member noteproperty -name Name -value $computer -force -passThru
#Creates a variable of user to ADSI object of the user $account on computer $computer.
[adsi]$user="WinNT://$computer/$account,user"
#This sets $user password to $password and writes the password to the object permanently.
$user.SetPassword($password)
$user.SetInfo()
#Error logic to determine if the above failed using special $error variable.
if (($Error | measure-object).count -ne $null){
Write-Host "Computer $computer failed admin password change!"
$computer | Add-Member noteproperty -name pwdchange -value "FAIL" -force -passThru
}
if (($Error | measure-object).count -eq $null){
Write-Host "Computer $computer admin password changed!"
$computer | Add-Member noteproperty -name pwdchange -value "SUCCESS" -force -passThru
}
#Clears $error variable for next computer in collection so above logic will still function.
$Error.clear()
}
#Displays the list of failed computers and creates a complete report in a csv file named $filename.
Write-Host "The following computers failed to have the local account changed. A complete report will be saved as $filename."
write-host ($computers | Where-Object {$_.pwdchange -ne "SUCCESS"})
$computers | select name,pwdchange | Export-Csv $filename -NoTypeInformation