繁体   English   中英

Powershell:向多个收件人发送 SMTP 消息

[英]Powershell: Sending SMTP message to multiple recipients

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 .

使用“1”参数调用“发送”的异常:“必须指定收件人。” 在 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 = "noreply@hdomain.com"
$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)

}

您是否验证了属性$User.EmailAddress不是 null? 我不知道您是否使用Get-ADUser命令将其添加到您的邮件消息 object 中。

我有一个类似的脚本,我像这样拉入所有 AD 用户:

[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)

从那里我遍历$samAccountNames以发送有关密码过期的电子邮件。

听起来像属性 EmailAddress 是 null..

尝试在代码中添加此行以查看属性是否真的为空:

$user.samaccountname
$user.EmailAddress

像这样:

Foreach ($User in $ExpiredUsers) {

$msg = new-object Net.Mail.MailMessage


$msg.From = "noreply@hdomain.com"
$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)

}

代码是正确的,但我必须改进我的 OU 搜索库。 搜索正在选择未填写 email 地址字段的非活动帐户,从而返回 null 错误。

谢谢大家。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM