我正在使用一个 powershell 脚本,该脚本将创建磁盘空间的 HTML 报告并将其作为电子邮件发送。不幸的是,我无法将脚本发送给多个电子邮件收件人。我正在使用的脚本可以在这里找到:
http://gallery.technet.microsoft.com/scriptcenter/6e935887-6b30-4654-b977-6f5d289f3a63
这是脚本的相关部分......
$freeSpaceFileName = "FreeSpace.htm"
$serverlist = "C:\sl.txt"
$warning = 90
$critical = 75
New-Item -ItemType file $freeSpaceFileName -Force
Function sendEmail
{ param($from,$to,$subject,$smtphost,$htmlFileName)
$body = Get-Content $htmlFileName
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}
$date = ( get-date ).ToString('yyyy/MM/dd')
$recipients = "to1@email.com", "to2@email.com"
sendEmail from@email.mail $recipients "Disk Space Report - $Date" smtp.server $freeSpaceFileName
我收到以下错误New-Object : Exception calling ".ctor" with "4" argument(s): "The specified string is not in the form required for an e
-mail address."
At E:\DiskSpaceReport.ps1:129 char:18
+ $msg = New-Object <<<< System.Net.Mail.MailMessage $from, $to, $subject, $body
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
最佳答案
您使用的 MailMessage 构造函数只需要一个电子邮件地址。请参阅 MSDN 文档
http://msdn.microsoft.com/en-us/library/5k0ddab0.aspx
您应该尝试使用 Send-MailMessage
而是因为它是 -To
参数接受地址数组Send-MailMessage -from from@email.mail -To $recipients -Subject "Disk Space Report - $Date" -smptServer smtp.server -Attachments $freeSpaceFileName
注意:Send-MailMessage 是在 PowerShell v2.0 中引入的,这就是为什么仍然有使用其他命令的示例的原因。如果您需要使用 v1.0,那么我会更新我的答案。
关于email - Powershell 脚本无法发送给多个收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256862/