<#

    ==Synopsis==

    Reset AD passwords using PowerSchool export.


    Author: Andrew Schott

    Company: Joletec, Inc.

    Last Modifed: 2020-10-28

#>


$VerbosePreference = 'Continue'

$transcriptPath = 'C:\ZimcoScripts\ResetPasswordsStudentsAD.log'



Import-Module ADSync


Start-Transcript -path $transcriptPath -append



$studentsFile = '\\ps\c$\ADSync\adstudents.csv'

$csvMeasure = Import-Csv $studentsFile | Measure-Object

[int]$csvCount = $csvMeasure.Count

$i = 0



import-csv $studentsFile -Header first_name,last_name,middle,grad_year,username,password | % {

    $SamAccountName = $_.username

    $Password = $_.password

    filter timestamp {"$(Get-Date -Format G): -- $i/$CSVcount $_"}

    

    if($SamAccountName -And $Password){

        # If records includes both username and password


        # Clear any errors from last record

        $error.clear()


        Try{

            Set-ADAccountPassword -Identity $SamAccountName -NewPassword (ConvertTo-SecureString $Password -AsPlainText -force)

        }Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{

            # AD user doesn't exist

            Write-Output "-- Failed! User not found: $SamAccountName" | timestamp

        }


        if (!$error) {

            # If no new error then show success

            Write-Output "-- Password Updated: $SamAccountName" | timestamp

        }

        

    }


    $i++

}


Stop-Transcript