Search code examples
azure-powershellazure-automation

Passing HashTable to PowerShell Runbook


I think, I read all related posts, still cannot make this work. I have an Azure Runbook that needs a HashTable parameter passed to a provisioning script. This is used later by the Apply-PnPTemplate function.

Parameters declared in the script as

[System.Collections.Hashtable] $Parameters = @{}

but I also tried

[Object] $Parameters = @{}

I try to test my script, add @{"customercode"="TEST"} as parameter, but I get this error message:

Cannot convert the "@{"customercode"="TEST"}" value of type "System.String" to type "System.Collections.Hashtable".

What I tried: passing with and without @, changing the delimiter to ;; (I need use this in PowerApps too) and ,, none of them helped. Please advise, what is the correct way of passing this object to the script.


Solution

  • I have experienced the same issue, seams that it always treats the input as string.

    I cannot figure out the reason, but here is a workaround: pass a fake hashtable(string type), then in the runbook, convert the string to hashtable.

    Demo code as below, hope it helps.

    param([string]$mystr)
    
    $mystr = $mystr.replace("=",":")
    
    # pass the string to hashtable
    
    $Parameters = @{}
    $jsonobj = $mystr | ConvertFrom-Json
    foreach($p in $jsonobj.psobject.properties){$Parameters[$p.name] = $p.value}
    
    #print the keys
    
    write-output "**the keys**"
    $Parameters.keys
    
    write-output "**the values**"
    
    #print the values
    $Parameters.values
    

    The parameters I passed: {"name"="jack","age"="11","city"="ddd"}

    The test result:

    enter image description here