I have some html code and I'd like to insert some variables from my powershell script into. When I insert the html in my script the script thinks its part of the script itself. What I'm trying to do is create signatures for my company that takes in user name, email, phone, ect and inserts this info into the html. I've got the variables set just having a hard time inserting them into the html.
2 Answers
It sounds like you want a "here-string" like this:
$my_html = @'
<html>
<head></head>
<body>
<p>Hi, my name is {0}, you can email me at {1} or call me at {2}.</p>
</body>
</html>
'@ -f $name, $email_addr, $phone_num
$my_html | out-file C:\my_html.html -encoding ascii
This technique combines the use of here-strings which allow you to use any character including quote characters and string formatting which put targets in the string that are replaced by the arguments giving to the -f operator. Then just pipe the string to file to open it in a browser.
Comments
Normally you can use a multi-line string for stuff like that:
$value1 = 'foo'
$value2 = 'bar'
$html = @"
<div>
<p>$value1</p>
<p>$value2</p>
<p>baz</p>
</div>
"@
Echoing $html should produce the following output:
<div>
<p>foo</p>
<p>bar</p>
<p>baz</p>
</div>
If this doesn't answer your question, please provide more information about what your HTML looks like before, and how it's supposed to look like after the insert, as well as any error you're getting.