Powershell:向多个收件人发送 SMTP 消息
Powershell: Sending SMTP message to multiple recipients
我正在尝试向多个电子邮件地址发送电子邮件(下面的完整代码用于上下文)并不断从 powershell 收到错误消息,不确定我做错了什么,但脚本没有传递每个电子邮件地址来自变量 $expiredusers 的用户。
使用“1”个参数调用 "Send" 异常:"A recipient must be specified."
在 C:\expiredreminder.ps1:33 char:5
+ $smtp.Send($味精)
+ ~~~~~~~~~~~~~~~~
+ 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
不为空?我不知道你是用 Get-ADUser
命令把它拉进来的,所以它没有被添加到你的邮件消息对象中。
我有一个类似的脚本,我像这样拉入所有 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 是空的..
尝试在代码中添加以下行以查看 属性 是否真的为空:
$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 搜索库。搜索正在寻找没有填写电子邮件地址字段的非活动帐户,从而返回空错误。
谢谢大家
我正在尝试向多个电子邮件地址发送电子邮件(下面的完整代码用于上下文)并不断从 powershell 收到错误消息,不确定我做错了什么,但脚本没有传递每个电子邮件地址来自变量 $expiredusers 的用户。
使用“1”个参数调用 "Send" 异常:"A recipient must be specified." 在 C:\expiredreminder.ps1:33 char:5 + $smtp.Send($味精) + ~~~~~~~~~~~~~~~~ + 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
不为空?我不知道你是用 Get-ADUser
命令把它拉进来的,所以它没有被添加到你的邮件消息对象中。
我有一个类似的脚本,我像这样拉入所有 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 是空的..
尝试在代码中添加以下行以查看 属性 是否真的为空:
$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 搜索库。搜索正在寻找没有填写电子邮件地址字段的非活动帐户,从而返回空错误。
谢谢大家