2015-09-11 33 views
0

我想用PowerShell將Send-MailMessage分配給多個用戶。 這工作:Send-MailMessage多個解析變量

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

我有一個變量$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++ 
} 

但是,當我跟我的變量$email更換-To,忽略了最低的工作。 (沒有錯誤,只需發送給$email中的第一位用戶)。

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

回答

1

Send-MailMessage

-To<String[]>指定將郵件發送地址。輸入姓名(可選)和電子郵件地址,例如「姓名」。該參數是必需的。

實施例:

send-mailmessage -from "..." -to "User02 <[email protected]>", "User03 <[email protected]>" -subject ... 

該命令發送一個電子郵件消息從用戶01到 2個其他用戶的附件。

我的意思是你需要使用,分隔符發送電子郵件給少數用戶。

correct "User02 <[email protected]>", "User03 <[email protected]>" 
wrong "User02 <[email protected]> , User03 <[email protected]>" 
0

正如@MrDywar已經指出的那樣,你要構造$email作爲一個逗號分隔的字符串:

"test <[email protected]>,test2 <[email protected]>" 

時,它需要一個字符串列表:

"test <[email protected]>", "test2 <[email protected]>" 

這應該會產生所需的結果:

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

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