Handling potential apostrophe's and quotes in a subscribed variable

Andrew Perry 21 Reputation points
2022-01-14T13:58:48.933+00:00

Hi,

I am bringing in data from ServiceNow "Description" field. Later on I have a powershell .NET activity that uses this subscribed variable from the previous activity.

But how would I handle the possibility that someone may type an apostrophe or a quote character in the description?

I first spotted this with a test I am doing "Andrew's Test" which of course the script failed when assigning that variable within single quotes. I resolved that by changing the single quotes in the script variable assignment to double quotes.

But thinking about this, this is now just going to fail if someone has a double quote, for the same reason.

So, the correct way would be to handle this properly regardless.

If I am doing something like this...

$snDescription = "{SUBSCRIBEDVARIABLEID}"

How can I capture within that if there are single or double quotes and escape them before assigning? Would I just have to do like...

"{SUBSCRIBEDVARIABLEID}".Replace(""", """") - I know this is wrong, but is the idea right? Or if not, how could I do this?

Thanks

Andrew

System Center Orchestrator
System Center Orchestrator
A family of System Center products that provide an automation platform for orchestrating and integrating both Microsoft and non-Microsoft IT tools.
267 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Stefan Horz 3,466 Reputation points
    2022-01-18T20:30:04.89+00:00

    Yes,
    just use:
    $snDescription = @'
    {subscribed Variable}
    '@

    1 person found this answer helpful.
    0 comments No comments

  2. Andrew Perry 21 Reputation points
    2022-01-17T18:14:33.117+00:00

    Just wanted to come back on this.

    The resolution for me was to use HERE-Strings in the script around the published data.

    Thanks to Chris who also seems to have had the exact same issue...

    https://www.concurrency.com/blog/w/using-here-strings-in-an-orchestrator-powershell-s

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.