azure-automationpowershell-workflow

How to iterate over Object properties of an input parameter


I have an Azure Automation Powershell Workflow:

workflow wf
{
  param(
    [parameter(Mandatory=$True)]
    [object] $p
  )
  inlinescript
  {
    # ...
  }
}

I am testing it using the Test pane and passing the value as {"FirstName": "John", "LastName": "Smith"}. I have also tried {"FirstName"="John";"LastName"="Smith"} and adding the @ in front of both cases.

According to this

If your runbook has an object type input parameter, then use a PowerShell hashtable with (name,value) pairs to pass in a value. For example, if you have the following parameter in a runbook: [Parameter (Mandatory = $true)][object] $FullName then you pass the following value to the parameter: @{"FirstName"="Joe";"MiddleName"="Bob";"LastName"="Smith"}

But in all of my tests $p is null.

How do I define an actual object, pass it in, and then iterate over the property names and values?


Solution

  • To access the input parameters within an activity of the PowerShell Workflow, you have to do via the $Using keyword. Once I figure that one out I was able to see the type was being passed in as a string. So I had to convert from a json string to an object by using ConvertFrom-Json cmdlet. I was then able to iterate through the properties by getting the Properties property of the PSObject:

    $pObj = $Using:p | ConvertFrom-Json
    ForEach ($pr in $pObj.PSObject.Properties)
    {
       #$pr.Name
       #$pr.Value
    }