Reputation: 25
I'm trying to email multiple email address (entire code below for context) and keep getting an error message from powershell, not sure what i'm doing wrong, but the script is not passing the email address of each user from variable $expiredusers.
Exception calling "Send" with "1" argument(s): "A recipient must be specified." At C:\expiredreminder.ps1:33 char:5 + $smtp.Send($msg) + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : InvalidOperationException
Import-Module ActiveDirectory
#Set the number of days within expiration. This will start to send the email x number of days before
it is expired.
$DaysWithinExpiration = 7
#Set the days where the password is already expired and needs to change.
$MaxPwdAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
$expiredDate = (Get-Date).addDays(-$MaxPwdAge)
#Set the number of days until you would like to begin notifying the users.
$emailDate = (Get-Date).addDays(-($MaxPwdAge - $DaysWithinExpiration))
#Filters for all users who's password is within $date of expiration.
$ExpiredUsers = Get-ADUser -Filter {(PasswordLastSet -lt $emailDate) -and (PasswordLastSet -gt
$expiredDate) -and (PasswordNeverExpires -eq $false) -and (Enabled -eq $true)} -Properties
PasswordNeverExpires, PasswordLastSet, Mail | select samaccountname, PasswordLastSet, @{name =
"DaysUntilExpired"; Expression = {$_.PasswordLastSet - $ExpiredDate | select -ExpandProperty Days}},
@{name = "EmailAddress"; Expression = {$_.mail}} | Sort-Object PasswordLastSet
Start-Sleep 5
Foreach ($User in $ExpiredUsers) {
$msg = new-object Net.Mail.MailMessage
$msg.From = "[email protected]"
$msg.To.Add($User.EmailAddress)
$msg.Subject = "blah blah subject"
$msg.Body = "blah blah message text"
$smtpServer = "smtpserver.domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)
}
Upvotes: 1
Views: 2969
Reputation: 25
The code was right, but I had to refine my OU search base. the search was picking up inactive accounts that did not have the email address field filled out, thus returning the null error.
Thanks everybody.
Upvotes: 0
Reputation: 124
sounds like the property EmailAddress is null..
try adding this lines on the code to see if the property is really empty:
$user.samaccountname
$user.EmailAddress
like this:
Foreach ($User in $ExpiredUsers) {
$msg = new-object Net.Mail.MailMessage
$msg.From = "[email protected]"
$msg.To.Add($User.EmailAddress)
$msg.Subject = "blah blah subject"
$msg.Body = "blah blah message text"
$user.samaccountname
$user.EmailAddress
$smtpServer = "smtpserver.domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#$smtp.Send($msg)
}
Upvotes: 1
Reputation: 500
Have you verified the property $User.EmailAddress
is not null? I don't know that you are pulling it in with your Get-ADUser
command so it's not getting added to your mail message object.
I have a similar script and I pull in all the AD users like this:
[String[]]$samAccountNames = $(Get-ADUser -filter { Enabled -eq $TRUE -and PasswordNeverExpires -eq $FALSE -and emailAddress -like "*.com"
} -Properties emailAddress,PasswordLastSet,PasswordExpired |
?{ ($_.PassWordLastSet.AddDays(59) -lt (get-date).AddDays(7)) -or $_.PasswordExpired } |
sort passwordLastSet)
And from there I iterate through $samAccountNames
to send the emails about passwords expiring.
Upvotes: 0