send-mailmessage cmdlet不接受列表变量

时间:2014-07-03 21:12:09

标签: email sharepoint sharepoint-2010 powershell-v2.0

我有一个包含人员列的sharepoint列表 - 我正在尝试编写一个PS脚本,该人员会在人员列中通过电子邮件发送给某人。现在我面临的问题是我无法将列表变量替换为send-mailmessage cmdlet i / e以下内容不起作用:

cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sourceWebURL = "http://sp2k10lab"
$sourceListName = "miztest"
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]

$spSourceItems = $spSourceList.GetItems()

$spSourceItems | ForEach-Object  {
$_name=$_['name']
$_email=$_['email']
send-mailmessage -from "support@XXXXXX" -to $_email -subject    "Regulatory Reminder Notice" -body "Tboooooooooooooooooooooooooitle is overdue by $_diff   days" -priority High -dno onSuccess, onFailure -smtpServer XXXXXXXx        
} 

$ _ email是电子邮件地址存储在列表中的位置 - 但是 - 当我尝试将其替换为我的send-mailmessage cmdlet时它不会 - sendmail-message cmdlet仅在我明确设置电子邮件地址时才有效即以下作品

    send-mailmessage -from "support@XXXXXX" -to "foo_miz <'abc@XXXXX.com'>" -subject        "Regulatory Reminder Notice" -body "Tboooooooooooooooooooooooooitle is overdue by $_diff   days" -priority High -dno onSuccess, onFailure -smtpServer XXXXXXX

我是否应该为此目的使用不同的cmdlet - 或者这只是我对我的语法愚蠢?

感谢您的帮助 - 真的很感激这样做的方法。

1 个答案:

答案 0 :(得分:0)

首先,你不应该在你的vars开头使用'_'字符,只是因为$_是PowerShell中的特定var。

其次,这是我使用Send-MailMessage的方式,它起作用:

      $mailServer = "smtp.myserver.com"
      $encoding=[System.Text.Encoding]::UTF8
      $cred = $null
      $body = "Are you serious !"

      $DesAdressesDest = "Nicolas S <nicolas.sc@Elyse.fs>","Francois H <francois.h@Elyse.fr>"
      $Subject = "About French bashing"

      send-mailmessage -SmtpServer $mailServer `
                       -to $DesAdressesDest `
                       -from "JP Blanc <jpb@people.com>"`
                       -subject $Subject `
                       -body $Body -BodyAsHtml `
                       -ErrorAction continue `
                       -Encoding $encoding