Home General Discussion

Handling ConvertFrom-Json in PowerShell

Tracie_MarshallTracie_Marshall Customer IT Monkey ✭
Hi Everyone,

We are passing a JSON object from an IT Request in our Portal to PowerShell in Orchestrator. However, we're having problems with certain characters within the object being handled as escape characters. When that happens we receive the following error:


We found a way to partially solve the problem for slashes as follows:

               $SRDesc1_Prep = $SR.Description1

               $SRDesc1_formatted = $SRDesc1_prep -replace '\\','\\'

               $UserResponses = ConvertFrom-Json $SRDesc1_formatted

However, the Orchestrator Runbook still fails if someone enters a double quote into the description field:


I can't find a good way around it since the PowerShell would need to look inside of the double quotes for another single quote. I was wondering if anyone had encountered a similar issue or knows of a better way to handle this?

Thank you!


Answers

  • Roland_KindRoland_Kind Partner Advanced IT Monkey ✭✭✭
    edited May 2018

    Hi,

    maybe another approach could be to replace all characters (except valid ones) with a space - or remove them.

    this example replaces all characters which are NOT in the list below with a space

    $newTitle=$newTitle -replace '[^a-zA-Z0-9\-[\]]', ' '

    hope this helps

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Roland's answer is very nice. Alternatively you could add a constraint to the Description Prompt in the offering to not allow those characters via a regex.
  • Tracie_MarshallTracie_Marshall Customer IT Monkey ✭
    Thank you Roland, I will have to test that out and see if I can get it to work..
    Geoff, how would we add a Regex to the description prompt? I haven't heard of using that before.
Sign In or Register to comment.