azureautomation / automation-packs

Collections of Automation resources that can include runbooks, configurations, modules, credentials, schedules, variables, connections, certificates, jobs, compilation jobs, and nodes.
MIT License
75 stars 42 forks source link

Json string for automation variable being converted to System.Object[] #20

Open Frankwayne opened 6 years ago

Frankwayne commented 6 years ago

I'm using an ARM template to pass in a json array object string for a runbook variable. After the ARM deployment the runbook variable is calling this a System.Object[] instead of a string object. This of course is not treated as a string anymore in runbook and causes my script to fail

When I pass in the string variable in the portal and declare it of a type string I am able to pull out my json string and convert it into an object just fine.

jefffanjoy commented 6 years ago

Is the input parameter for the runbook of type [object]? If so, try changing it to type [psobject] and see if that makes any difference. I know when using the portal [psobject] type will allow a json to be automatically parsed into a structured object.

Frankwayne commented 6 years ago

I suppose I need to clarify what I'm saying.

I'm attempting to create a runbook variable using an ARM template. I pass in a json String for the value of my runbook variable deploy via an ARM template. The final runbook variable type is of System.Object[] and not the value type of String like I expect it to be. Documentation states that we should only be allowed to upload the following variable types

String Integer DateTime Boolean Null

https://docs.microsoft.com/en-us/azure/automation/automation-variables

This is how I'm creating my jsonString to be passed into my arm template.

$jsonString= $mainObject| convertTo-Json -Depth 5 -compress $jsonString = '"' + $jsonString+ '"'

I ended up changing my string to a base64 encoding though to get around this issue. It look like there are a couple layers of escaping the required " for json property fields to store inside of a runbook variable.

If this is a valid bug though I can work with you on how to recreate this issue.

ScoutmanPt commented 3 years ago

Howdy , just in case some still hitting this problem, here's how to solve it :

  1. Replace any " with /" -> $jsonString = $jsonString .Replace('"','\"')
  2. Then set your string to append " (as @Frankwayne suggested) ->$jsonString = '"' + $jsonString + '"'

Full example :

$jsonString ='{"element1":"value1","element2":"value2"'} $jsonString = $jsonString | ConvertFrom-Json $jsonString = $jsonString | ConvertTo-Json -Compress $jsonString = $jsonString .Replace('"','\"') $jsonString = $jsonString .Replace('{','{\n') $jsonString = $jsonString .Replace('}','\n}') $jsonString = $jsonString .Replace('",','",\n') $jsonString = '"' + $jsonString + '"'

(the \n = newline ->to have a nice formated json )

This works fine, I'm using it for creating automation account variables, i've seen the double quote problem across the entire azure stack, this procedure overcome the issue in Powershell.

If you're using the az cli is slightly different:

  1. Replace any " with \\" -> $jsonString = $jsonString .Replace('"','\\"')
  2. Then set your string to use append " ->$jsonString = '"\"' + $jsonString + '\""'

$jsonString ='{"element1":"value1","element2":"value2"'} $jsonString = $jsonString | ConvertFrom-Json $jsonString = $jsonString | ConvertTo-Json -Compress $jsonString = $jsonString.Replace('"','\\"') $jsonString = $jsonString .Replace('{','{\n"') $jsonString = $jsonString .Replace('}','\n}"') $jsonString = $jsonString .Replace('",','",\n') $jsonString = '"\"' + $jsonString + '\""'

Hope it helps .

Buzz if you come across any issue.

Best regards