在PowerShell中创建多行电子邮件正文

时间:2015-11-05 17:12:51

标签: email powershell

当我尝试实现多行电子邮件正文时,我一直收到错误。我怀疑语法错误。无法在线找到任何示例。有什么建议吗?

错误:Unexpected token 'EmployeeName"] to $AccountExpire"' in expression or statement.

$subject = "Email for $item["EmployeeName"]. Date expire $AccountExpire"
$body=@"                            
Name:  $item["Employee"]
Class: Contractor
Depart: $item["Depart"]
Region: $item["Region"]
Manager: $item["Manager"]
New Date: $NewDate                          
"@                      
SendUpdateEmail($subject,$Body)

1 个答案:

答案 0 :(得分:5)

您需要使用子表达式($())转义那些数组索引操作:

$subject = "Email for $($item["EmployeeName"]). Date expire $AccountExpire"

对于多行字符串(或 here-strings ,因为它们被正式调用)也是如此:

$body=@"                            
Name:  $($item["Employee"])
Class: Contractor
# and so on...                 
"@

就个人而言,我会选择多行模板并使用-f格式运算符填写值:

$bodyTemplate=@'
Name: {0}
Class: Contractor
Depart: {1}
Region: {2}
Manager: {3}
New Date: {4}
'@
$body = $bodyTemplate -f $item["Employee"],$item["Depart"],$item["Region"],$item["Manager"],$NewDate

使用-f时,您还可以格式化不同类型的数据,因此如果$NewDate[DateTime]对象,您可以控制模板内部的格式,例如:< / p>

@'
Date: {0:HH:mm:ss}
'@ -f (Get-Date)

会产生:

Date: 14:55:09

(假设你下午五点到三点这样做了)