I’m looking for the power shell script which sends email notifications to password expired users based on AD.
So, we have office 365 sync with on-premise AD for user syncronizations with AAD connector.
I found this below script but its not working.
#Import AD Module
Import-Module ActiveDirectory
#Create warning dates for future password expiration
$SevenDayWarnDate = (get-date).adddays(7).ToLongDateString()
#Email Variables
$MailSender = " Password AutoBot <myemailid@mycompany.com>"
$Subject = ‘FYI - Your account password will expire soon’
$EmailStub1 = ‘I am a bot and performed this action automatically. I am here to inform you that the password for’
$EmailStub2 = ‘will expire in’
$EmailStub3 = ‘days on’
$EmailStub4 = ‘. Please contact the helpdesk if you need assistance changing your password. DO NOT REPLY TO THIS EMAIL.’
$SMTPServer = ‘smtp.office365.com’
#Find accounts that are enabled and have expiring passwords
$users = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0 } </strong> <strong> -Properties "Name", "EmailAddress", "msDS-UserPasswordExpiryTimeComputed" | Select-Object -Property "Name", "EmailAddress",
@{Name = “PasswordExpiry”; Expression = {[datetime]::FromFileTime($_.“msDS-UserPasswordExpiryTimeComputed”).tolongdatestring() }}
#check password expiration date and send email on match
foreach ($user in $users) {
if ($user.PasswordExpiry -eq $SevenDayWarnDate) {
$days = 7
$EmailBody = $EmailStub1, $user.name, $EmailStub2, $days, $EmailStub3, $SevenDayWarnDate, $EmailStub4 -join ’ '
Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServer $SMTPServer -Subject $Subject -Body $EmailBody
}
else {}
}
I found no errors when i executed this script in AD to analysis issues. still its not sending emails to password expired users.
I have only replaced this line in above script : $MailSender = " Password AutoBot <myemailid@mycompany.com>" with my email ID.
1.my email has Multi factor authentication for login. does this stopping this to send emails ?
2.What if i create one email or shared mailbox without MFA and use ?
3.any changes need to be done in script ?