Runbook parameters do not handle complex types properly

Stephen Jennings 1 Reputation point
2022-10-14T15:55:38.213+00:00

When passing parameters to Runbooks, neither PowerShell 5.1 nor PowerShell 7.1 handle parameter types appropriately. It appears that whatever strings I send in are passed through ConvertFrom-Json even if I want the parameter interpreted as a string.

According to the documentation, I should be able to pass JSON as a parameter that has type [object]. The documentation then uses ConvertFrom-Json to turn that into an object. But, when I pass JSON to my runbook, it is either already converted (and sometimes to an incorrect shape), or the JSON is corrupted and ConvertFrom-Json cannot parse it.

Using the following runbook:

param(  
 [string]$StringContainingText,  
 [string]$StringContainingJsonString,  
 [string]$StringContainingJsonArray,  
 [string]$StringContainingJsonObject,  
 [object]$Object,  
 [array]$Array  
)  
  
"StringContainingText: $($StringContainingText.GetType()) ~$StringContainingText~" | Write-Output  
"StringContainingJsonString: $($StringContainingJsonString.GetType()) ~$StringContainingJsonString~" | Write-Output  
"StringContainingJsonArray: $($StringContainingJsonArray.GetType()) ~$StringContainingJsonArray~" | Write-Output  
"StringContainingJsonObject: $($StringContainingJsonObject.GetType()) ~$StringContainingJsonObject~" | Write-Output  
  
"Object: $($Object.GetType()) ~$Object~" | Write-Output  
"Array: $($Array.GetType()) ~$Array~ Len: $($Array.Length) Len[0]: $($Array[0].Length)" | Write-Output  

And starting the runbook with this HTTP request:

{  
  "properties": {  
    "runbook": {  
      "name": "ParameterTest"  
    },  
    "parameters": {  
      "StringContainingText": "Hello, world",  
      "StringContainingJsonString": "\"Hello, world\"",  
      "StringContainingJsonArray": "[ \"one\", \"two\" ]",  
      "StringContainingJsonObject": "{ \"foo\": \"bar\" }",  
      "Object": "{ \"foo\": \"bar\" }",  
      "Array": "[ \"foo\", \"bar\", 123 ]"  
    },  
    "runOn": ""  
  }  
}  

A PowerShell 5.1 runbook produces the following output:

StringContainingText: string ~Hello, world~  
StringContainingJsonString: string ~Hello, world~  
StringContainingJsonArray: string ~one two~  
StringContainingJsonObject: string ~@{foo=bar}~  
Object: System.Management.Automation.PSCustomObject ~@{foo=bar}~  
Array: System.Object[] ~foo bar 123~ Len: 3 Len[0]: 3  

The PowerShell 5.1 result has the following problems:

  • The string parameter StringContainingJsonString is missing the double-quotes at the beginning and end of the string
  • The string parameter StringContainingJsonArray has is missing the square brackets and double-quotes
  • The string parameter StringContainingJsonObject is completely mangled (it looks like it was converted to a PS hashtable then back to a string)

A PowerShell 7.1 runbook produces the following output:

StringContainingText: string ~Hello, world~  
StringContainingJsonString: string ~Hello, world~  
StringContainingJsonArray: string ~[ one, two ]~  
StringContainingJsonObject: string ~{ foo: bar }~  
Object: string ~{ foo: bar }~  
Array: System.Object[] ~[ foo, bar, 123 ]~ Len: 1 Len[0]: 17  

The PowerShell 7.1 result has the following problems:

  • The string parameter StringContainingJsonString is missing the double-quotes at the beginning and end of the string
  • The string parameter StringContainingJsonArray has is missing the square brackets and double-quotes
  • The string parameter StringContainingJsonObject is completely mangled (it looks like it was converted to a PS hashtable then back to a string)
  • The object parameter Object should be a hashtable, but instead is a string. The double quotes have been stripped so it is no longer valid JSON.
  • The array parameter Array should contain 3 elements but instead contains 1 element which is the whole object mangled the same way StringContainingJsonArray is.
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,110 questions
{count} votes