PowerShell将Send-MailMessage发送给计划任务中的多个收件人

时间:2014-07-23 06:59:43

标签: arrays powershell arguments

我编写了自己的函数,以HTML格式向最终用户发送电子邮件。如果我在我的脚本中使用以下格式To: ,这对一个用户或多个用户都无瑕疵:

$MailTo = "<User1@domain.com>", "<User2@domain.com>"

但是,当我尝试在Windows中使用相同的参数启动计划任务时,PowerShell会抱怨Cannot find an object with identity User1@domain.com或邮件仅发送给一个用户,而不是两者都发送。

我按计划任务或提示启动脚本的方式,这不起作用:

powershell -File "S:\Script\Send_Mail.ps1" -MailTo "<User1@domain.com>", "<User2@domain.com>"

即使我尝试其中一个参数,也只发送一封邮件:

-MailTo "<User1@domain.com>, <User2@domain.com>"
-MailTo "User1@domain.com, User2@domain.com"

我找到了this,但我不明白我做错了什么。谢谢你的帮助。

脚本:

[CmdletBinding(SupportsShouldProcess=$True)]
Param (
      [parameter(Mandatory=$true,Position=0)]
      [String[]] $MailTo,
      [parameter(Mandatory=$true,Position=1)]
      [String[]] $Subject,
      [parameter(Mandatory=$true,Position=2)]
      [String[]] $Message1
    )

Function Send-Mail {

[CmdletBinding(SupportsShouldProcess=$True)]
Param (
      [parameter(Mandatory=$true,Position=0)]
      [String[]] $To,
      [parameter(Mandatory=$true,Position=1)]
      [String[]] $Subject,
      [parameter(Mandatory=$true,Position=2)]
      [String[]] $Message1,
      [parameter(Mandatory=$false,Position=3)]
      [String[]] $Message2
    )

# Set SMTP server name
$PSEmailServer = "SMTP.Server"

# Check where we've been launched from
if($PSISE) {
  $LaunchedFrom = "Windows PowerShell ISE"
}
else {
     $LaunchedFrom = "Scheduled task or console (not ISE)"
}

$HTML = @"
<!DOCTYPE html>
<html><head><style type="text/css">
body {font-family:verdana;background-color:white;}
h1 {background-color:black;color:white;margin-bottom:5px;}
h2 {background-color:lightGrey;margin-top:5px;}
h3 {margin-left:10px;font-size: 14px;}
p {font-size: 14px;margin-left:10px;}
p.italic {font-style: italic;font-size: 12px;}
table, td, th {font-size: 14px;border-collapse: collapse; border: 1px lightGrey; padding: 3px; text-align: left; padding-right:10px;}
li {font-size: 14px;}
base {target="_blank"}
</style></head><body>
<h1>&nbsp;$(if(!$ScriptName){"Test"}else{$ScriptName})</h1>&nbsp;
<h2>&nbsp;The following has been reported:</h2>
<p>$Message1</p>
<p>$Message2</p>
<h2>&nbsp;About</h2>
<p><table>
$(if($ScriptStart){$("<tr><th>Start time</th><td>{0:00}/{1:00}/{2:00} {3:00}:{4:00} ({5})</td></tr>" -f `
$ScriptStart.Day,$ScriptStart.Month,$ScriptStart.Year,$ScriptStart.Hour,$ScriptStart.Minute,$ScriptStart.DayOfWeek)})
$(if($ScriptRunTime){"<tr><th>Total runtime</th><td>$ScriptRunTime</td></tr>"})
$(if($LogFolder){"<tr><th>Log folder</th><td>$("<a href=`""$LogFolder"`">$LogFolder</a>")</td></tr>"})
$(if($PSCommandPath){"<tr><th>Script location</th><td>$PSCommandPath</td></tr>"})
<tr><th>Launched from</th><td>$LaunchedFrom</td></tr>
</tr><tr><th>Server</th><td>$env:computername</td></tr>
</tr></tr><tr><th>Whoami</th><td>$("$env:USERDNSDOMAIN\$env:USERNAME")</td>
</tr><tr><th>Help</th><td><a href="http://www.google.com" target="_blank">Google</a></td></tr>
</table></p><p class=italic>"$(Get-Content "S:\Input\Extra\Quotes.txt" | Get-Random -ErrorAction SilentlyContinue)"</p1>
</body></html>
"@

# Mail content
Write-Verbose "Function Send-Mail: Sending mail"        
Send-MailMessage -To $To -From "$(if(!$ScriptName){"Test"}else{$ScriptName}) PowerShell@$env:computername" `
-Subject "$Subject" `
-BodyAsHtml "$HTML"
}


Send-Mail $MailTo $Subject $Message1

解决方案

更正参数以仅接受需要的数组(多个$MailTo):

Param (
      [parameter(Mandatory=$true,Position=0)]
      [String[]] $MailTo,
      [parameter(Mandatory=$true,Position=1)]
      [String] $Subject,
      [parameter(Mandatory=$true,Position=2)]
      [String] $Message1
    )

要从Windows中的计划任务运行此功能,您可以使用以下格式:

powershell "S:\Script\Send_Mail.ps1" -ImportFile "S:\Script\Servers.csv" -MailTo "User1@domain.com", "User2@domain.com"
powershell -ExecutionPolicy Bypass "S:\Script\Send_Mail.ps1" -ImportFile "S:\Script\Servers.csv" -MailTo "User1@domain.com", "User2@domain.com"

这些格式不被接受,会导致语法错误,或者并非所有用户都收到他们的电子邮件:

powershell -File "S:\Script\Send_Mail.ps1" -ImportFile "S:\Script\Servers.csv" -MailTo "User1@domain.com, User2@domain.com"
powershell -File "S:\Script\Send_Mail.ps1" -ImportFile "S:\Script\Servers.csv" -MailTo '"User1@domain.com", "User2@domain.com"'
powershell "S:\Script\Send_Mail.ps1" -ExecutionPolicy Bypass -ImportFile "S:\Script\Servers.csv" -MailTo "User1@domain.com, User2@domain.com"

上面的所有行都可以粘贴在PowerShell ISE中进行测试,因此您可以查看错误,而不是依赖于计划任务来告诉您。我希望它可以帮助你们,因为它吓坏了我:P

2 个答案:

答案 0 :(得分:1)

尝试这样:

powershell -File "S:\Script\Send_Mail.ps1" -MailTo "'User1@domain.com', 'User2@domain.com'"

答案 1 :(得分:0)

您可以像这样定义“To”变量:

$emailTo = @('Name <name1@domain.com>', 'Name2 <name2@domain.com>')

然后您可以在函数Send-MailMessage

中使用它

示例:

$servername = hostname
$smtpserver = 'localhost'
$emailTo = @('username1 <user1@dom.com>', 'username2<user2@dom.com>')
$emailFrom = 'SomeServer <user3@dom.com>'
Send-MailMessage -To $emailTo -Subject 'Low available memory' -Body 'Warning' -SmtpServer $smtpserver -From $emailFrom