2014-09-22 73 views
0

我创建了一个先进的功能来发送基础上,通过answer proposed Mjolinor电子邮件。但是,我接受HTML代码块时遇到困难。这是抱怨System.String格式,不能转换为System.Management.Automation.SwitchParameterPowerShell的发送,MAILMESSAGE BodyAsHTML错误类型

对于我的HTML代码我使用的是这里的字符串,使用时像Send-MailMessage -BodyAsHtml $HTML但不是当我尝试它,如下工作正常。我似乎无法弄清楚如何转换它,所以它的工作。

错误:

An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.. 
At S:\Test.ps1:168 char:9 
+   $EmailParams.keys | Where {$EmailParams.$_ -eq $null} | foreach {$EmailP ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Collecti...tableEnumerator:HashtableEnumerator) [], RuntimeException 
    + FullyQualifiedErrorId : BadEnumeration 

Failed to send email to [email protected] due to: Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'BodyAsHtml'. 

代码:

$HTML = @" 
<!DOCTYPE html> 
<html><head><style type="text/css"> 
body {font-family:verdana;background-color:white;} 
h1 {background-color:black;color:white;margin-bottom:5px;} 
h2 {background-color:lightGrey;margin-top:5px;} 
h3 {margin-left:10px;font-size: 14px;} 
p {font-size: 14px;margin-left:10px;} 
p.italic {font-style: italic;font-size: 12px;} 
table, td, th {font-size: 14px;border-collapse: collapse; border: 1px lightGrey; padding: 3px; text-align: left; padding-right:10px;} 
li {font-size: 14px;} 
base {target="_blank"} 
</style></head><body> 
<h1>&nbsp;$(if(!$ScriptName){"Test"}else{$ScriptName})</h1>&nbsp; 
<h2>&nbsp;The following has been reported:</h2> 
$Messages 
</body></html> 
"@ 

     $EmailParams = @{ 
To   = $To 
Cc   = $Cc 
From  = $From 
Subject  = $Subject 
BodyAsHtml = $HTML 
Priority = $Priority 
SMTPServer = $SMTPserver 
Attachments = $Attachment 
ErrorAction = 'Stop' 
} 
$EmailParams.keys | Where {$EmailParams.$_ -eq $null} | foreach {$EmailParams.Remove($_)} 
Try { Send-MailMessage @EmailParams } 
Catch { "Failed to send email to $($To) due to: $_" } 
Finally {} 

This person有类似的问题,但这只是来一个System.String-Body

谢谢您的帮助。

有两件事情错在这里:

  1. BodyAsHtml是一个布尔($True),所以你需要使用Body提供您的HTML代码。
  2. 你不能立即从哈希表中删除的东西。所以你需要2个foreach循环。

完整的答案:

$HTML = @" 
<!DOCTYPE html> 
<html><head><style type="text/css"> 
body {font-family:verdana;background-color:white;} 
h1 {background-color:black;color:white;margin-bottom:5px;} 
h2 {background-color:lightGrey;margin-top:5px;} 
h3 {margin-left:10px;font-size: 14px;} 
p {font-size: 14px;margin-left:10px;} 
p.italic {font-style: italic;font-size: 12px;} 
table, td, th {font-size: 14px;border-collapse: collapse; border: 1px lightGrey; padding: 3px; text-align: left; padding-right:10px;} 
li {font-size: 14px;} 
base {target="_blank"} 
</style></head><body> 
<h1>&nbsp;$(if(!$ScriptName){"Test"}else{$ScriptName})</h1>&nbsp; 
<h2>&nbsp;The following has been reported:</h2> 
$Messages 
</body></html> 
"@ 

     $EmailParams = @{ 
      To   = $To 
      Cc   = $Cc 
      Bcc   = $Bcc 
      From  = $From 
      Subject  = $Subject 
      Body  = $HTML 
      BodyAsHtml = $True 
      Priority = $Priority 
      SMTPServer = $SMTPserver 
      Attachments = $Attachments 
      ErrorAction = 'Stop' 
     } 

     $list = New-Object System.Collections.ArrayList 

     foreach ($h in $EmailParams.Keys) { 
      if ($($EmailParams.Item($h)) -eq $null) { 
       $null = $list.Add($h) 
      } 
     } 

     foreach ($h in $list) { 
      $EmailParams.Remove($h) 
     } 

     Try { 
      Send-MailMessage @EmailParams 
      Write-Verbose "Send-Mail: Sending mail to: $To" 
     } 
     Catch { 
      "Failed to send email to $($To) due to: $_" 
     } 
+1

感谢。我正在考虑直接从powershell发送电子邮件(使用HTML模板),这非常有帮助。我想补充一点,你可以改进最终的解决方案。列表块可以完全删除并替换为这个,干杯:[System.Collections.ArrayList] $ EmailParams.keys | ? {$ EmailParams。$ _ -eq $ null} | %{$ EmailParams.Remove($ _)} – Lorek 2016-12-20 00:47:21

回答

3

如有疑问,请阅读documentationBodyAsHtml是一个布尔值,指示主体是否包含HTML。您仍然需要通过Body参数传递实际身体。更改此:

$EmailParams = @{ 
    To   = $To 
    Cc   = $Cc 
    From  = $From 
    Subject  = $Subject 
    BodyAsHtml = $HTML 
    Priority = $Priority 
    SMTPServer = $SMTPserver 
    Attachments = $Attachment 
    ErrorAction = 'Stop' 
}

到这一点:

$EmailParams = @{ 
    To   = $To 
    Cc   = $Cc 
    From  = $From 
    Subject  = $Subject 
    Body = $HTML 
    BodyAsHtml = $true 
    Priority = $Priority 
    SMTPServer = $SMTPserver 
    Attachments = $Attachment 
    ErrorAction = 'Stop' 
}
+0

谢谢Ansgar,这真的解决了'-BodyAsHtml'的问题。很愚蠢,我自己也没看到。但是,当我不使用'Attachment'时,通过集合枚举时发生错误仍然存​​在。很奇怪.. – DarkLite1 2014-09-22 11:02:21

+0

显然,这个错误总是弹出,当值之一为空和'的foreach {$ EmailParams.Remove($ _)}执行'。奇怪的是,事后删除空参数并发送邮件,虽然它显示一个红色的错误。我甚至无法用'Out-Null'消除它。 – DarkLite1 2014-09-22 11:24:04

+0

@ DarkLite1错误消息被打印到错误输出流,而“Out-Null”重定向成功输出流。使用'2> $ null'或'2>&1 | OUT-Null'。有关更多信息,请参见[这里](http://blogs.technet.com/b/heyscriptingguy/archive/2014/03/30/understanding-streams-redirection-and-write-host-in-powershell.aspx)。 – 2014-09-22 12:32:03

相关问题