Hello,
I have the following script. Everything works fine except the part that it is supposed to send the email to the user. The part that send an email to the supervisor is working properly. I took the original form to a website and I edited it myself. I'm not a master of powershell but I have some basic knowledge. If you could help me to see where is the error that would be awesome. The goal is to send an email, everyday, as soon as the user is at 14 days from having his password expired. There is some addon that send me a report of the Active Directory every morning since it is enforced to a task every morning. I'm hiding personal information for security purposes. Some things are in french. Sorry about that.
Thanks in advance for your precious help.
Configuration des variables
$smtpServer= "server SMTP@hidden.com"
$expireindays = 14
$from = "random@email.com"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "C:\Users\administrator\Desktop\passwordexpirationlog.csv" # EX: c:\Expiration.csv
$testing = "Disabled" #Mode de test Set to Disabled pour que l'usager reçoit un courriel d'avis
$testRecipient = "random@email.com"
$SendToSupervisor = "Enabled" # Set to Enabled Pour que le superviseur reçoit le fichier de logto Disable Logging
$Supervisor = "random@email.com" # Le superviseur recevra le fichier de log
$Expiration = "Yes"
###########################################################
########################################################
Vérification de la journalisation
if (($logging) -eq "Enabled")
{
Test Log File Path
$logfilePath = (Test-Path $logFile)
if (($logFilePath) -ne "True" -or $SendToSupervisor -eq "Enabled" )
{
if (Test-Path $logFile -PathType leaf)
{
Remove-Item $logFile
}
Create CSV File and Headers
New-Item $logfile -ItemType File
Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn,Notified"
}
} # End Logging Check
Paramètre du systeme
$textEncoding = [System.Text.Encoding]::UTF8
$date = Get-Date -format ddMMyyyy
Fin de vérification des paramètres du systeme
Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress | where {$.Enabled -eq "True"} | where {$.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
$DefaultmaxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
Process Each User for Password Expiry
foreach ($user in $users)
{
$Name = $user.Name
$emailaddress = $user.emailaddress
$passwordSetDate = $user.PasswordLastSet
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
$sent = "" # Reset Sent Flag
Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
else
{
No FGP set to Domain Default
$maxPasswordAge = $DefaultmaxPasswordAge
}
$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
Set Greeting based on Number of Days to Expiry.
Check Number of Days to Expiry
$messageDays = $daystoexpire
if (($messageDays) -gt "1")
{
$messageDays = "in " + "$daystoexpire" + " days."
$messageJours = "dans " + "$daystoexpire" + " jours."
}
else
{
$messageDays = "today."
$messageJours = "Aujourd'hui"
}
Email Subject Set Here
$subject="Votre mot de passe va expirer $messageJours | Your password will expire
$messageDays"
Email Body Set Here, Note You can use HTML, including Images.
$body ="
À l'attention de $name,
<p> Votre mot de passe réseau va expirer $messageJours<br>
Pour changer votre mot de passe sur un ordinateur quand vous êtes au bureau, appuyez sur
CTRL+ALT+Delete et sélectionnez « changer de mot de passe ». <br>
Pour changer votre mot de passe sur un ordinateur quand vous êtes à la maison, votre VPN doit être
actif, appuyez sur CTRL+ALT+Delete et sélectionnez « changer de mot de passe ». Assurez-vous d'être connecter au VPN pour le faire, sauf si vous êtes au bureau. <br>
<p>Merci, <br>
</P>
Dear $name,
<p> Your Password will expire $messageDays<br>
To change your password on a computer when you are at the office, press CTRL+ALT+Delete
and choose « Change Password ». <br>
To change your password on a computer when you are at home, your VPN need to be active ,
press CTRL+ALT+Delete and choose « Change Password ». Make sure you are connected with the VPN unless you are at the office. <br>
<p>Thanks, <br>
</P>"
If Testing Is Enabled - Email Administrator
if (($testing) -eq "Enabled")
{
$emailaddress = $testRecipient
} # End Testing
If a user has no email address listed
if (($emailaddress) -eq $null)
{
$emailaddress = $testRecipient
}# End No Valid Email
Send Email Message
if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
{
$sent = "Yes"
$Expiration = "Yes"
If Logging is Enabled Log Details
if (($logging) -eq "Enabled")
{
Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent"
}
Send Email Message
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding $textEncoding
} # End Send Message
else # Log Non Expiring Password
{
$sent = "No"
If Logging is Enabled Log Details
if (($logging) -eq "Enabled")
{
Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent"
}
}
} # End User Processing
Si $Logging et $SendToSupervisor sont a "Enabled le fichier de log est envoyer au superviseur seulement si un mot de passe envoie d'expiration est trouvé
if (($logging) -eq "Enabled" -and $SendToSupervisor -eq "Enabled" -and $Expiration -eq "Yes" )
{
$Subject = "Rapport d'expiration des mots de passe des usagers"
$Body = "Bonjour, Voici le rapport d'expiration des mots de passe. Merci!"
#write-host $Subjet
#write-host $Body
#write-host $Supervisor
#write-host $logFile
#write-host $smtpServer
#Pause
Send-MailMessage -to $Supervisor -subject $Subject -body $Body -smtpserver $SMTPServer -from $From -Attachments $logFile
#write-host "Courriel envoyé"
}
End