PowerShell Encoding issue in 7.8.3



  • I have a PowerShell script that was written in Notepad++ and has been working properly for some time. I recently made a change to the script and it stopped working. Digging in, it wasn’t the change I made, but the portion of the script that sends an email with HTML formatting. If I take the script and copy/paste into PowerShell ISE and save, it works fine.

    Problem line:

    $EmailBody += '<font size="3" color="red"><b>Failed to enable Remote Mailbox ' + $AdUser.FirstName + " " + $AdUser.LastName + " (" + $AdUser.userPrincipalName + ').</font></b><br><br>'
    

    PowerShell Error:

    At C:\Scripts\Activate-NewAccounts.ps1:238 char:30
    + ... <font size="3" color="red"><b>Failed to enable Remote Mailbox ' + $Ad ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unexpected token '3" color="red"><b>Failed to enable Remote Mailbox ' + $AdUser.FirstName + "' in expression or statement.
    At C:\Scripts\Activate-NewAccounts.ps1:238 char:30
    +         $EmailBody += '<font size="3" color="red"><b>Failed to enable ...
    +                                    ~
    Missing closing ')' in expression.
    

    Since a straight copy/paste/save in PowerShell ISE solves the issue with no changes, it appears there’s something in the PowerShell save encoding in the latest version.


Log in to reply