# Domain kullanicilarina parolalarinin suresi dolmadan once e-posta bildirimleri gonderebilen powershell scripti
# AD kullanicilarini sorgulama
$Users = Get-ADUser -Filter { Enabled -eq $True -and PasswordNeverExpires -eq $False } `
-Properties DisplayName, EmailAddress, msDS-UserPasswordExpiryTimeComputed `
-SearchBase ‘OU=bt,DC=cinar,DC=local’ |
Select-Object -Property DisplayName, EmailAddress, @{Name=”ExpirationDate”;Expression={[datetime]::FromFileTime($_.”msDS-UserPasswordExpiryTimeComputed”)}} |
Sort-Object -Property “ExpirationDate”
# Parolanin suresinin yakinda dolup dolmayacagini kontrol edip ve e-posta bildirimi gondermek.
$UserList = foreach ($User in $users) {
if ($User.ExpirationDate -le (Get-Date).AddDays(30) -and $User.ExpirationDate -ge (Get-Date))
{
# Parolasi yakinda sona erecek kullanicilarin listesini kaydetmek icin PSCustomObject olusturmak
[PSCustomObject]@{
Name = $User.DisplayName
EmailAddress = $User.EmailAddress
ExpiryDate = $User.ExpirationDate
}
# Email Gonderimi
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$SMTP = “smtp-mail.outlook.com”
$From = “admin@outlook.com”
$username = “admin@outlook.com”
#Read-Host -Prompt “mail sifresi giriniz” -AsSecureString | ConvertFrom-SecureString | Out-File “C:\Shared\admincred.txt”
$Pass = Get-Content “C:\Shared\admincred.txt” | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $Pass
$Subject = “Oturum Acma Sifreinizin Suresi yakinda Dolmak Uzere !!!”
$Body = “Dear $($User.DisplayName),” + “`n`nOturum acma sifrenizin suresi $User.ExpirationDate. tarihinde dolacaktir.Lutfen yakin zamanda degistririniz.” + “`n`nSaygilarimizda,” + “`nIT Departmani”
$EmailBody = “Hello $($User.DisplayName),” + “`n`n” +
“Bu Sifrenizin $($User.ExpirationDate).tarihinde sona erecegini hatirmak icindir” + “`n`n” +
“Hesabiniza erisim saglayabilmek icin lutfen sifrenizi degistiriniz.” + “`n`n” +
“Teseekkurler,” + “`n” +
“IT Departmani”
# E-Postayi gondermeye calisma,istisnalari yakalama ve gunluge kaydetme
Try {
# Send-MailMessage -From $From -To $User.EmailAddress -Subject $Subject -Body $EmailBody -smtpserver $SMTP -usessl -Credential $cred -Port 587
$EmailMessage = @{
From = $From
To = $User.EmailAddress
Subject = $Subject
Body = $EmailBody
SmtpServer = $SMTP
UseSsl = $True
Credential = $cred
Port = 587
}
Send-MailMessage @EmailMessage
# Log success
Add-Content -Path “C:\Shared\Logs\EmailSuccess.log” -Value “Email Basariyla gonderdildi.kullanici: $($User.DisplayName) -Email: $($User.EmailAddress) Gonderim Tarihi: $(Get-Date) “
}
Catch {
# Loglari dosyaya yazdir.
Write-Host “Email Gonderilemedi.Kullanici: $($User.DisplayName) -Email: $($User.EmailAddress): $_”
Add-Content -Path “C:\Shared\Logs\EmailFailure.Log” -Value “Email Gonderilemedi.Kullanici: $($User.DisplayName) -Email $($User.EmailAddress) Tarih: $(Get-Date) $_”
}
}
}
$Userlist | sort-object ExpirationDate
sensin kötü
test