Powershell Script Send Mail For Expire User


Send e-mail to users who their password  will be expire  in  “n” days. Script  must be run  in AD server.

############ SMTP Server Configuration ##########################
$smtpServer="YourSMTPServerIPAddress"
$from = "email-noreply@casesup.com"
$expireindays = 10
###################################################################################################################
#### Get Users From AD Who are enabled ###########################################################################
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties * |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
foreach ($user in $users)
{
$Name = (Get-ADUser $user | foreach { $_.Name})
$emailaddress = $user.emailaddress
$passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
$subject="Bilgisayar parolanızın süresinin dolmasına $daystoExpire gün kaldı"
$body ="
Sayın $name,
<p> Bilgisayara giriş yaparken kullandığınız parolanın geçerlilik süresi <b>$daystoexpire</b> gün sonra sona erecektir.<br>
Parola değişikliğinizi, bu süre içerisinde <b>Ctrl + Alt + Delete</b> tuş kombinasyonunu tuşlayıp gelen menüde <b>Change a password</b> butonuna tıklayarak yapabilirsiniz.
<br>Ofis dışında iken şifre değişimi için https://casesup.com adresini kullanabilirsiniz.<br><br>Saygılarımızla<hr>
Destek için yardım masası ekibimizden (helpdesk@casesup.com) yardım alabilirsiniz.
<p>
</P>"
if ($daystoexpire -lt $expireindays)
{
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High -Encoding ([System.Text.Encoding]::UTF8)
}
}

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me