Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell – replace variable with it's content in HTML file

I have HTML file:

1.html

!DOCTYPE html>
<html>
    <head>
        <title>Password Reminder</title>
    </head>
    <body>
    
        <p>
        Dear user, your password expires in: <strong>$($days)</strong> days.
        </p>
    </body>
   </html>

I created function which reads file content and replace $days variable with actual variable value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function ReadTemplate($days) {
    $template_content = Get-Content "C:\PasswordReminder\1.html" -Encoding UTF8 -Raw
    #$template_content = [IO.File]::ReadAllText($template)
    $template_content = $template_content -replace "{}",$days
    return $template_content
}

But when calling it

$content = ReadTemplate -days 2

Instead of Dear user, your password expires in: 2 days.

I’m getting

Dear user, your password expires in: $($days) days.

Instead of $($days) specified {0} but nothing

>Solution :

Try $template_content = $template_content.replace('$($days)',$days)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading