Milad
Milad

Reputation: 125

Changing the color of some specifc text if its exist in HTML

i m looking for a way to get change the color of a text if its there. The Idea is that, i have some text output from powershell that i want to send to Email with use of -BodyAsHTML.

$body = "
    <html><body>
        <font color='FF0000'>$alarmMessage</font><br /><br />
    <body><html>
    "

    Send-MailMessage -to $recipients -SmtpServer "TEST" -from "TEST" `
                     -Subject "TEST" -BodyAsHTML $body -Encoding ([System.Text.Encoding]::UTF8) 

alarmMessage has all the text in it. so i want to check if the alarmMessage has something like "not recieved", so only ""not recieved" should be red.

how do i do that?

Upvotes: 1

Views: 160

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174760

If you only want to change the style of part of the $alarmMessage text, then the surrounding body template is probably not the best place to store the styling element.

Instead, use the -replace operator to add a <span> (or <font>) element to the relevant part of the alarm message itself:

# sample alarm message
$alarmMessage = 'The expected data was not received within the time limit'

# inject html styling
$alarmMessage -replace '\b(not\s+received)\b','<span style="color: #FF0000">$1</span>'

# expand message in html body template
$body = @"
<html><body>
    <p>$alarmMessage</p><br /><br />
<body><html>
"@

Upvotes: 2

Related Questions