【发布时间】:2016-07-21 07:20:03
【问题描述】:
我正在尝试在 PowerShell 中向多个收件人发送电子邮件。
我知道 PowerShell 1.0 方式,但我不想使用它。
我的邮件功能是这样的
function Send-Mail() {
Param (
# email attributes
[string]$smtpServer,
[string]$from,
[string[]]$to,
[string]$subject,
[string]$body
)
Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from
}
我可以做到这一点:
$smtpServer = "email.server.local"
$from = "sender@email.com"
$subject = "Subject"
$body = "Test"
Send-Mail $smtpServer $from ("user1@email.com", "user2@email.com") $subject $body
..但是如果我放了
$to = "user1@email.com", "user2@email.com"
Send-Mail $smtpServer $from $to $subject $body
邮件只发送给第二个收件人
如果我在函数中本地设置$to,这也可以正常工作,所以问题在于将参数传递给函数。
【问题讨论】:
-
我知道 PowerShell 1.0 方式但我不想使用它是什么意思。 是什么意思?
-
您使用的是哪个版本的 PowerShell?
Get-Host。我没有看到并在这里发布您的第二个示例,这对我有用。 -
您在当前会话中是否有过
$to的强烈要求?$to.GetType().fullname -
啊哈.....那将是一个问题。它应该是
system.object[]或system.string[]。保存您的代码并使用新会话重新打开 ISE/PS。那它行得通吗?如果您强烈转换该变量,则以后如果不先删除它就无法更改它。Remove-Variable to -
您在此之前的评论促使我做一个
rm variable:to。刚刚重新运行代码并且它正在工作。非常感谢,因为从昨晚开始这一直困扰着我。呸!干杯。
标签: function powershell powershell-4.0