PowerShell Send-Mailmessage BodyAsHTML 错误类型

Dar*_*te1 0 parameters powershell type-conversion

我创建了一个高级功能,可以根据Mjolinor提出答案发送电子邮件。但是,我在让它接受 HTML 代码块时遇到了困难。它抱怨System.String无法转换为System.Management.Automation.SwitchParameter.

对于我的 HTML 代码,我使用了一个 here 字符串,当使用 like 时它可以正常工作,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 User@domain.net due to: Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'BodyAsHtml'. 
Run Code Online (Sandbox Code Playgroud)

编码:

$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 {}
Run Code Online (Sandbox Code Playgroud)

此人也有类似的问题,但也仅仅是走到一个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: $_"  
        }
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 5

如有疑问,请阅读文档BodyAsHtml是一个布尔值,指示正文是否包含 HTML。您仍然需要通过Body参数传递实际主体。改变这个:

$EmailParams = @{
  To          = $To
  Cc          = $Cc
  From        = $From
  Subject     = $Subject
  BodyAsHtml  = $HTML
  Priority    = $Priority
  SMTPServer  = $SMTPserver
  Attachments = $Attachment
  ErrorAction = 'Stop'
}
Run Code Online (Sandbox Code Playgroud)

进入这个:

$EmailParams = @{
  To          = $To
  Cc          = $Cc
  From        = $From
  Subject     = $Subject
  Body        = $HTML
  BodyAsHtml  = $true
  Priority    = $Priority
  SMTPServer  = $SMTPserver
  Attachments = $Attachment
  ErrorAction = 'Stop'
}
Run Code Online (Sandbox Code Playgroud)