EvotecIT / PSWinReporting

This PowerShell Module has multiple functionalities, but one of the signature features of this module is the ability to parse Security logs on Domain Controllers providing easy to use access to AD Events.
MIT License
705 stars 70 forks source link

Colors & Styles not working with accents and other non-ascii characters #80

Open Indigo744 opened 7 months ago

Indigo744 commented 7 months ago

Tested on the Legacy script (V1). I didn't test on the V2 version.

For a workaround, see below.

If a formatting value (be it for color or style) is using accent or other non-ascii characters, then the color or style won't be applied.

Since event log are localized, accented characters can appeared in the report (French in our case).

Given a formatting option below:

$FormattingParameters = @{
    Colors = @{
        Green = 'ajouté', 'activé' # 'added', 'activated'
    }
}

It does not highlight the word: image

The issue is not linked to the file encoding, as UTF8-BOM is used and the accent is correctly encoded as we can see in the log: image

The issue is related to the way the replace function is actually called: https://github.com/EvotecIT/PSWinReporting/blob/36089a85e19c8758102e4e3e9bc81737b1760238/Private/PSWinReportingEmails.ps1#L62-L64

We can see the Replace function is called on the Body variable which contains the HTML body.

Accents (and others non-ascii characters) are usually html-encoded to avoid rendering issue. We can quickly see this is indeed the case here when looking at the raw HTML output: image

The solution would be to html-encode the color/style values before trying to replace them in the HTML body.

Workaround

For a simple workaround, simply drop these two lines before calling Start-ADReporting (assuming the formatting options are in the $FormattingParameters variable):

$FormattingParameters.Colors.clone().Keys | % { $FormattingParameters.Colors[$_] = $FormattingParameters.Colors[$_] | % { [System.Web.HttpUtility]::HtmlEncode([System.Web.HttpUtility]::HtmlDecode($_)) } }
$FormattingParameters.Styles.clone().Keys | % { $FormattingParameters.Styles[$_] = $FormattingParameters.Styles[$_] | % { [System.Web.HttpUtility]::HtmlEncode([System.Web.HttpUtility]::HtmlDecode($_)) } }

It simply iterates over all colors/styles values and html-encode them. (Note that I left the HtmlDecode in case somebody run these lines multiple time)

With that, the colors and styles are now working 😸 image