@@ -4,28 +4,37 @@ param (
44 [string]$newp
55)
66
7+
78Function Test-ADAuthentication {
89 param(
9- $username,
10- $password)
11-
12- (New-Object DirectoryServices.DirectoryEntry "",$username,$password).psbase.name -ne $null
10+ [string]$username,
11+ [string]$password)
12+ ((New-Object DirectoryServices.DirectoryEntry -ArgumentList "",$username,$password).psbase.name) -ne $null
1313}
14-
15- $test=Test-ADAuthentication -username $user -password $oldp
16- if ($test -eq $false){
17- Write-Host "Invalid password"
18- exit 1
19- }
20-
21- try{
22- Set-ADUser -Identity $user -CannotChangePassword $false
23- Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
24- Set-ADUser -Identity $user -CannotChangePassword $true -PasswordNeverExpires $true
25- }catch{
26- Write-Host $_
14+ {% if dataStatus == -2 %}
15+ Set-ADUser -Identity $user -Enabled $True
16+ {% endif %}
17+ $userFound=get-aduser -Filter "SamAccountName -eq 'jcerri' -and Enabled -eq '$True' -and PasswordExpired -eq '$False'"
18+ if ( ! $userFound ){
19+ Write-Host 'user not active or not found or password expired'
2720 exit 1
2821}
29- Set-ADUser -Identity $user -CannotChangePassword $true
30- WriteHost "Password changed"
31- Exit 0
22+ if (Test-ADAuthentication -username "$user" -password "$oldp"){
23+ Write-Host "password ok"
24+ try{
25+ Set-ADUser -Identity $user -CannotChangePassword $false
26+ # on lance 2 fois la commande car ne marche pas la premiere fois bug 2019 ?
27+ Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
28+ Set-ADAccountPassword -Identity $user -NewPassword (ConvertTo-SecureString -AsPlainText $newp -Force) -reset
29+ Set-ADUser -Identity $user -CannotChangePassword $true -PasswordNeverExpires $true
30+ }catch{
31+ Write-Host $_
32+ exit 1
33+ }
34+ Set-ADUser -Identity $user -CannotChangePassword $true
35+ Write-Host "Password changed"
36+ Exit 0
37+ }else{
38+ Write-Host "Authentication Invalid password"
39+ exit 1
40+ }
0 commit comments