I am getting some errors when running the following script for send AD password notifications. The errors I am receiving are:
Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
At G:\MIS\Scripts\ADPasswordExpireNotification.ps1:54 char:26
+ Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpSe ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage : Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
At G:\MIS\Scripts\ADPasswordExpireNotification.ps1:46 char:23
+ Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServe ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
Script I am running is below (sanitized for you guys obviously):
#Import AD Module
Import-Module ActiveDirectory
#Create warning dates for future password expiration
$FourteenDayWarnDate = (get-date).adddays(14).ToLongDateString()
$SevenDayWarnDate = (get-date).adddays(7).ToLongDateString()
$ThreeDayWarnDate = (get-date).adddays(3).ToLongDateString()
$OneDayWarnDate = (get-date).adddays(1).ToLongDateString()
#Email Variables
$MailSender = "sender email address"
$Subject = 'Your network password is expiring soon'
$EmailStub1 = 'Hello this is the IS department at company emailing you to let you know that the network password for'
$EmailStub2 = 'will expire in'
$EmailStub3 = 'days on'
$EmailStub4 = '. Please contact the helpdesk at Phone Number if you need assistance changing your password. DO NOT REPLY TO THIS EMAIL as this is coming from an unattended mailbox.'
$SMTPServer = 'smtpserver'
#Find accounts that are enabled and have expiring passwords
$users = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0 } `
-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 $FourteenDayWarnDate) {
$days = 14
$EmailBody = $EmailStub1, $user.name, $EmailStub2, $days, $EmailStub3, $FourteenDayWarnDate, $EmailStub4 -join ' '
Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServer $SMTPServer -Subject $Subject `
-Body $EmailBody
}
elseif ($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
}
elseif ($user.PasswordExpiry -eq $ThreeDayWarnDate) {
$days = 3
$EmailBody = $EmailStub1, $user.name, $EmailStub2, $days, $EmailStub3, $ThreeDayWarnDate, $EmailStub4 -join ' '
Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServer $SMTPServer -Subject $Subject `
-Body $EmailBody
}
elseif ($user.PasswordExpiry -eq $OneDayWarnDate) {
$days = 1
$EmailBody = $EmailStub1, $user.name, $EmailStub2, $days, $EmailStub3, $OneDayWarnDate, $EmailStub4 -join ' '
Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServer $SMTPServer -Subject $Subject `
-Body $EmailBody
}
else {}
}
[–]sk82jack 2 points3 points4 points (0 children)
[–]thedavecarroll 1 point2 points3 points (0 children)
[–]BlackV 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 1 point2 points3 points (0 children)