Using Here-Strings in an Orchestrator PowerShell Script

by Christopher Mank

On top of all of the out-of-box integration with all of the other products in the System Center suite, Orchestrator allows another level of integration by allowing you to write custom PowerShell (PS) scripts to accomplish pretty much anything your heart desires. While working with a client this past week, I ran into an interesting issue while working with single and double quotes. Their requirement was to take the Description field from a Service Manager Incident (which is entered in free-form by the user on the Self Service Portal), add some formatting to it and email it to the proper Support Group. Simple right? Well, at first, it was. I simply created a Runbook that would get the Incident and then run a PS script to add the formatting before running the Send Email activity. In the PS script I assigned the Incident's Description to a $Desc variable, enclosed by double (") quotes like the picture below and proceeded with my formatting. This worked just fine for a couple weeks until we noticed it was failing from time to time. After some investigation, we found that users were adding double quotes (") to their descriptions on the portal. For example: My monitor is having issues, it is a 24" monitor. See the issue? Since the description has a double quote, when we go to assign it to our $Desc variable which is already enclosed in double quotes, the assignment fails since PS sees too many quotes. I thought to myself, well this is an easy fix, I'll just enclose the Description in Orchestrator with single quotes (') like the picture below. Yeah...that only worked for a few days until we started seeing failures since users started entering in single quotes in their descriptions. For example: My computer is not working, right now I am using Terrance's spare computer. See the issue with this one? Since the description has a single quote, when we go to assign it to our $Desc variable which is already enclosed in single quotes, the assignment fails since PS sees too many quotes. So now what do we do since we don't know if the user will enter single quotes, double quotes, or even both!The solution is using Here-Strings. A Here-String is a PowerShell construction that helps you bypass these complexities and even accounts for multi-line string values (To find out more about PS Here-Strings, check out this link.). So to solve our issue with the Incident Description, I simply wrapped it in a Here-String. Once we did this, not matter what kind of quotes the user would use in their description, it would get assigned to the variable and formatted correctly every time. So the moral of the story is, if you plan to assign published data to a variable in an Orchestrator PowerShell script and there is potential that the data may contain single or double quotes, use a Here-String to assign the variable correctly. Until the Whole World Hears, Christopher