error when running powershell script to send ad password expiry notification

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 {}

}

4 comments · 100% upvoted · other discussions

comments

sk82jack

Do you get any responses from the following?:

Get-ADUser -filter 'Enabled -eq $true -and PasswordNeverExpires -eq $false -and PasswordLastSet -gt 0 -and EmailAddress -notlike "*"'
thedavecarroll

Just a quick glance, is EmailAddress a returned property? The attribute mail should be the default SMTP address returned via Get-ADUser.

BlackV

what does $user.EmailAddress return

Lee_Dailey

howdy myndhack,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee