Experts out there !
I am running the below script to send out email notification to all AD users whose password is going expire (mainly for the remote users who do not login to domain), below script does not throw any errors. powerGUI shows script execution is successfull and we do receive an email.
The issue with the below script is we receive an empty email with the subject "Password change log for", the body of the message "Password change log for" and the attachment with txt file is empty. no information is written to the logs.
Appreciate if anyone can coorect the script.
========================================================================
#Add the Quest PowerShell snapin
Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Get-QADUser -SearchRoot "OU=Test Accounts,OU=Accounts,OU=Cityname,OU=Xyz,DC=Mydomain"
Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} |
ForEach-Object {
$today = Get-Date
$logdate = Get-Date -format yyyyMMdd
$samaccountname = $_.samAccountName
$mail = $_.mail
$passwordstatus = $_.PasswordStatus
$passwordexpiry = $passwordstatus.Replace("Expires at: ","")
$passwordexpirydate = $passwordexpiry
$daystoexpiry = ($passwordexpirydate - $today).Days
$attachment = "C:\passwordexpiry.txt"
$emailFrom = "adadmin@mydomain.com"
$body = "Please change your password to prevent loss of access to your *my domain* systems`n`n"
$body += "If you are unable to change your password, please contact the help desk at ext 000 or 000.000.0000"
if ($daystoexpiry -lt 9 )
{
$emailTo = "$mail"
$subject = "Your Network password will expire in $daystoexpiry day(s) please change your password."
Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -Attachments $attachment -SmtpServer "mail.mydomain.com"
Write-Host "Email was sent to $mail on $today"
Add-Content C:\maillog$logdate.txt "Email was sent to $mail on $today"
}
}
Send-MailMessage -To "adadmin@mydomain.com" -From "adadmin@mydomain.com" -Subject "Password change log for $today" -Body "This is the log
from $today" -Attachments "C:\maillog$logdate.txt" -SmtpServer "mail.mydomain.com"
==========================================================
Thanks in Advance
Naveen Rao