Populating Variables in External Text File

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
pfgustafsson
Posts: 6
Last visit: Tue Jan 02, 2024 10:41 am

Populating Variables in External Text File

Post by pfgustafsson »

Product, version and build: PowerShell Studio v5.5.150
32 or 64 bit version of product: 64 bit
Operating system: Win 10
32 or 64 bit OS: 64 Bit

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

Here's a question I had after reading the blog post on storing powershell variables within an external text file.
https://www.sapien.com/blog/2018/03/22/ ... nal-files/

After the reading the post, I was also working on a project where I wanted to do the reverse of what the article was talking about and have variables within a text file grab variable values from within the script.

Here’s the scenerio:
I have a list of new mailbox accounts and want to inform all the users of their new email address and credentials (sent to their old email address). I want to utilize a ‘form style’ text document with html formatting for the -BodyAsHtml switch in the Send-MailMessage cmdlet and push variable values into the body of the message. I can successfully include the html formatted message in the body of the message however, none of the variables get populated. There must be away to read the message body first and inject the variable values before sending the message. Question is, how?

Any thoughts?
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Populating Variables in External Text File

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populating Variables in External Text File

Post by jvierra »

Example:

Code: Select all

$body = @'
<pre>
    Dear  {0},
     Your free gift of {1} for being a dear customer
     has been shipped on {2}
<\pre>
'@
$body -f 'Jane Dough', 'vanilla cake', '12/31/2031'
https://blogs.technet.microsoft.com/hey ... owershell/

The "-f" operator is the string formatter operator and works as a "sprintf" function.
This topic is 5 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked