title: PowerShell Send-Mailmessage BodyAsHTML wrong type
body: <p>I've created an advanced function to send out e-mails based on the <a href="https://stackoverflow.com/questions/19639763/run-built-in-cmdlet-with-parameters-that-may-or-may-not-be-specified">answer proposed</a> by Mjolinor. However, I'm facing difficulties to have it accept the HTML code block. It's complaining about the <code>System.String</code> format that cannot be converted to <code>System.Management.Automation.SwitchParameter</code>.</p>
  
  <p>For my HTML-code I'm using a here string, which works fine when used like <code>Send-MailMessage -BodyAsHtml $HTML</code> but not when I try it as below. And I can't seem to figure out how to convert it so it does work.</p>
  
  <p><strong>The error:</strong></p>
  
  <pre><code>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'. 
  </code></pre>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>$HTML = @"
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;&lt;head&gt;&lt;style type="text/css"&gt;
  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"}
  &lt;/style&gt;&lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;&amp;nbsp;$(if(!$ScriptName){"Test"}else{$ScriptName})&lt;/h1&gt;&amp;nbsp;
  &lt;h2&gt;&amp;nbsp;The following has been reported:&lt;/h2&gt;
  $Messages
  &lt;/body&gt;&lt;/html&gt;
  "@
  
          $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 {}
  </code></pre>
  
  <p><a href="http://technodrone.blogspot.com/2010/04/those-annoying-thing-in-powershell.html" rel="nofollow noreferrer">This person</a> had a similar problem, but that was only to come to a <code>System.String</code> for the <code>-Body</code>.</p>
  
  <p>Thank you for your help.</p>
  
  <p>There are 2 things wrong here:</p>
  
  <ol>
  <li><code>BodyAsHtml</code> is a boolean (<code>$True</code>), so you need to use <code>Body</code> to provide your HTML code.</li>
  <li>You can't immediately remove stuff from a hashtable. So you need 2 <code>foreach</code> loops.</li>
  </ol>
  
  <p><strong>Full answer:</strong></p>
  
  <pre><code>$HTML = @"
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;&lt;head&gt;&lt;style type="text/css"&gt;
  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"}
  &lt;/style&gt;&lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;&amp;nbsp;$(if(!$ScriptName){"Test"}else{$ScriptName})&lt;/h1&gt;&amp;nbsp;
  &lt;h2&gt;&amp;nbsp;The following has been reported:&lt;/h2&gt;
  $Messages
  &lt;/body&gt;&lt;/html&gt;
  "@
  
          $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: $_"  
          }
  </code></pre>