Summary: Discovering some of the awesome and neat ways to leverage a Here-String in a Script or function
Q: Hey, Doctor Scripto!
I used to write scripts with supporting configuration files that never really changed. I was wondering if there was any way to put them INSIDE the script directly to make it self contained.
A: Hello AB, I was chatting with my good friend Sean on this one. Here he talks about using Here-Strings to solve this problem.
Thanks Doctor Scripto, yes a Here-String can be a great way to embed the configuration or other data for that matter in a script.
If you’ve never encountered a Here-String before, this is an example.
Hello people, this is Doctor Scripto
Welcome to the Scripting Blog
Today we're showing you a Here-String.
When you examine the properties of $Message you’ll see it not an Array but one long string (including carriage returns and linefeed characters within)
Now if the Here-String is enclosed within Single Quotes (just like any other string in PowerShell) it is taken as a literal (Nothing is interpreted). Why you could almost say a Here-String written in this fashion is truly “What You See Is What You Get”.
What is nice about this is if you wanted something in the script to be editable by a NON scripter, such as a welcome message, Placing it in a Literal Here-String makes it much easier for a non tech to do it.
Another example of a Here-String is when you enclose it with Double Quotes (“). When it’s done this way the content is now Evaluated in the parser before it is consumed by the rest the script. Here’s a simple example such as new user Letter
Hello and welcome to Contoso $FirstName !
Your user id on the system will be $UserID with a Temporary Password of $TemporaryPassword
and your home Office is in $Office. You can be reached at $PhoneNumber.
If you have any issues with your setup you can call 425-555-1111 in order to reach the Help desk.
Your Friendly Neighborhood It Department
If you ran this and went to display the object $WelcomeLetter , instead of seeing Variables it would all expand to real values like this.
Again something that’s a bit easier to edit and view. But how about an array of information? Normally I would read an array in this fashion.
But for a person who might NOT be a PowerShell Guru, wouldn’t it be cool to place that array in this fashion?
We can take this string and convert it to an array with a simple use of -replace in PowerShell (to clear out the LineFeeds in the Here-String (ASCII 10).
$SomeList=$SomeListToUse -replace "`n" , ''
Then we can use split method by targeting the “CarriageReturn” (ASCII 13) at the termination of the string to convert it to an Array.
Now an interesting thing to note when converting a Here-String to an array in PowerShell core is the Linefeed character is not used in this environment (think of standard Text file in a Unix environment, you probably won’t see a linefeed anywhere in it)
In which case our conversion would look like this
We could even put in a CSV list in a script in this fashion (Just as if it were an external CSV file) and keep it completely self-contained.
$CSVList=$CSVListTemp -replace "`n" , ''
$CSVData=$CSVList | ConvertFrom-CSV
Of course these are just examples of ways you COULD use a Here-String within a script, not how you MUST.
But I thought it would be interesting to share some thoughts for those of you unfamiliar with Here-Strings. They do provide some interesting options in PowerShell
AB that is all there is to using Here-String for the moment. But who knows? They may be more to discover next Week!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.
Your good friend, Doctor Scripto
PowerShell, Doctor Scripto, Sean Kearney
The post Maximizing the Power of Here-String in PowerShell for Configuration Data appeared first on Scripting Blog.