简体   繁体   English

Send-MailMessage多个解析变量

[英]Send-MailMessage multiple parsing variable

I want to Send-MailMessage with PowerShell to multiple users. 我想将带有PowerShell的Send-MailMessage给多个用户。 This works: 这有效:

Send-MailMessage -To "test@mail.fr", "test2@mail.fr" -Subject "TEST BASE INCIDENT SCRIPT" -Body "TEST" -SmtpServer $emailSMTP -BodyAsHtml -Credential $emailCredential -Encoding $emailEncoding

I have a variable $email I had construct like this: 我有一个像这样构造的变量$email

$email = ''
$c = 0
foreach ($currentValue in $value) {
    $id = $currentValue.LookupId
    $spUserManager = $spweb.AllUsers.GetByID($id)
    if ($c -gt 0) {
        $email += ","+($spUserManager.Name + " <" + $spUserManager.Email + ">") 
    } else {
        $email += ($spUserManager.Name + " <" + $spUserManager.Email + ">") 
    }
    $c++
}

But when I replace -To with my variable $email , it dosen't work. 但是,当我用变量$email替换-To时,它不起作用。 (no error, just send to the first user in $email ). (没有错误,只需发送给$email的第一个用户)。

Send-MailMessage -To $email -Subject "TEST BASE INCIDENT SCRIPT" -Body "TEST" -SmtpServer $emailSMTP -BodyAsHtml -Credential $emailCredential -Encoding $emailEncoding

Send-MailMessage or ss64 Send-MailMessagess64

-To<String[]> Specifies the addresses to which the mail is sent. -To<String[]>指定邮件发送到的地址。 Enter names (optional) and the e-mail address, such as "Name ". 输入名称(可选)和电子邮件地址,例如“名称”。 This parameter is required. 此参数是必需的。

Example: 例:

send-mailmessage -from "..." -to "User02 <user02@example.com>", "User03 <user03@example.com>" -subject ...

This command sends an e-mail message with an attachment from User01 to two other users. 此命令将一封带有附件的电子邮件从User01发送给其他两个用户。

I mean you need to use , separator to send e-mail to few users. 我的意思是,你需要使用,分离器将电子邮件发送到几个用户。

correct "User02 <user02@example.com>", "User03 <user03@example.com>"
wrong "User02 <user02@example.com> , User03 <user03@example.com>"

As @MrDywar already pointed out you're constructing $email as a single comma-separated string: 正如@MrDywar指出的那样,您正在将$email构造为单个逗号分隔的字符串:

"test <test@mail.fr>,test2 <test2@mail.fr>"

when it needs to be a list of strings: 当它需要一个字符串列表时:

"test <test@mail.fr>", "test2 <test2@mail.fr>"

This should produce the desired result: 这应该产生期望的结果:

$email = foreach ($currentValue in $value) {
    $spUserManager = $spweb.AllUsers.GetByID($currentValue.LookupId)
    $spUserManager.Name + " <" + $spUserManager.Email + ">"
}

Send-MailMessage -To $email -Subject ...

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

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