powershelloutlookwindows-11

Powershell Problem in passing a variable to $mail.HTMLBody, the mail is empty when using a $variable but works when its direct string = "asbdas"


i have a mail script but since changeing from windows 10 to windows 11 my script is not working anymore.

I could already figgure out that this exact step is not working (either with the ps7 or the default ps from windows 11). Previously i was using Windows 10 default ps where it was working..

that is the following code as example:


$outlook = New-Object -comObject Outlook.Application
$mail = $outlook.CreateItemFromTemplate(some_template.oft)

$mail.Display()
# opens up the mail in a window for "live" view of the change

$mail.HTMLBody =  "asdasd"
# Working flawlesly, the Mail has the String in it

$body = "asdasd"
$mail.HTMLBody =  $body 
# not working, mail is empty

i couldnt find anything on the web or i dont have the right words for searching. When using the $variable the operator '=' is not working anymore .. i tried with all sorts of ConvertTo or Convert From or the Out-String stuff

Maybe someone have a solution. Best thanks


Solution

  • In a pristine session, the code in your question would not produce the symptom, however, as verified in Outlook version 16.0.0.17628, there appears to be a bug with respect to assigning non-string values to the .HTMLBody property, which is unrelated to whether the value is assigned directly or via a variable:


    As a workaround for the bug as well as to generally ensure that a string is assigned to .HTMLBody, use a [string] cast:

    $outlook = New-Object -ComObject Outlook.Application
    $mail = $outlook.CreateItemFromTemplate(some_template.oft)
    
    $mail.Display()
    # opens up the mail in a window for "live" view of the change
    
    # Sample $body value that can NOT be used directly.
    $body = 'one', 'two'
    
    # Casting to [string] ensures that a single string is assigned.
    # The array elements are space-concatenated.
    $mail.HTMLBody = [string] $body
    

    Note: