Rob Gething Posted July 21, 2021 Posted July 21, 2021 I am using the following code to get taskAnswers: # Define output stream type [OutputType([object])] # Define runbook input params Param ( # API Params #[Parameter (Mandatory= $true)] [string] $taskId } # Add XMLMC params Add-HB-Param "taskId" $taskId $false # Invoke XMLMC call, output returned as PSObject $xmlmcOutput = Invoke-HB-XMLMC "task" "taskGetInfo" $exceptionName = "" $exceptionSummary = "" # Read output status if($xmlmcOutput.status -eq "ok") { if($xmlmcOutput.params.title -and $xmlmcOutput.params.title -ne ""){ $title = $xmlmcOutput.params.title $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.options -and $xmlmcOutput.params.options -ne ""){ $options = $xmlmcOutput.params.options $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.createdOn -and $xmlmcOutput.params.createdOn -ne ""){ $createdOn = $xmlmcOutput.params.createdOn $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.createdBy -and $xmlmcOutput.params.createdBy -ne ""){ $createdBy = $xmlmcOutput.params.createdBy $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.owner -and $xmlmcOutput.params.owner -ne ""){ $owner = $xmlmcOutput.params.owner $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.outcomes -and $xmlmcOutput.params.outcomes -ne ""){ $outcomes = $xmlmcOutput.params.outcomes $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.taskAnswers -and $xmlmcOutput.params.taskAnswers -ne ""){ $taskAnswers = $xmlmcOutput.params.taskAnswers $requestWarnings = $xmlmcOutput.params.warnings } } # Build resultObject to write to output $resultObject = New-Object PSObject -Property @{ Status = $xmlmcOutput.status Error = $xmlmcOutput.error ExceptionName = $exceptionName ExceptionSummary = $exceptionSummary title = $title options = $options createdOn = $createdOn createdBy = $createdBy owner = $owner outcomes = $outcomes taskAnswers = $taskAnswers } if($resultObject.Status -ne "ok" -or $exceptionName -ne ""){ Write-Error $resultObject } else { Write-Output $resultObject } I can see that taskAnswers is an array, but I am struggling to return: taskAnswers: [{key: "OtherCorrectName", value: "Pipeline", displayValue: "Pipeline"},…]0: {key: "OtherCorrectName", value: "Pipeline", displayValue: "Pipeline"} I've tried a PSCustomObject with with a ForEach-Object to loop through the array, but this doesn't return any results. Having run $taskAnswers | Get-Member, taskAnswers is Boolean so with only return true or false.
SamS Posted July 21, 2021 Posted July 21, 2021 @Rob Gething, The resulting string is a JSON array of objects. You will need to convert it first with PowerShell. https://www.google.com/search?client=firefox-b-d&q=powershell+convert+JSON+to+array+of+objects
Rob Gething Posted July 21, 2021 Author Posted July 21, 2021 @SamS, If I do: ConvertFrom-Json -InputObject $taskAnswers, I get: ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null. If I do: $taskAnswers | ConvertFrom-Json $answers = $taskAnswers | ConvertFrom-Json $answers[0].key $answers[0].value $answers[0].displayValue I get: Cannot index into a null array. It's as if $taskAnswers has not data to pull back, but I can see it in inspect element: taskAnswers: [{key: "OtherCorrectName", value: "Pipeline", displayValue: "Pipeline"},…]0: {key: "OtherCorrectName", value: "Pipeline", displayValue: "Pipeline"} displayValue: "Pipeline" key: "OtherCorrectName" value: "Pipeline"
Rob Gething Posted July 26, 2021 Author Posted July 26, 2021 Final working code for the benefit of others: # Define output stream type [OutputType([object])] # Define runbook input params Param ( # API Params #[Parameter (Mandatory= $true)] [string] $taskId ) # Add XMLMC params Add-HB-Param "taskId" $taskId $false # Invoke XMLMC call, output returned as PSObject $xmlmcOutput = Invoke-HB-XMLMC "task" "taskGetInfo" $exceptionName = "" $exceptionSummary = "" # Read output status if($xmlmcOutput.status -eq "ok") { if($xmlmcOutput.params.title -and $xmlmcOutput.params.title -ne ""){ $title = $xmlmcOutput.params.title $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.options -and $xmlmcOutput.params.options -ne ""){ $options = $xmlmcOutput.params.options $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.createdOn -and $xmlmcOutput.params.createdOn -ne ""){ $createdOn = $xmlmcOutput.params.createdOn $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.createdBy -and $xmlmcOutput.params.createdBy -ne ""){ $createdBy = $xmlmcOutput.params.createdBy $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.owner -and $xmlmcOutput.params.owner -ne ""){ $owner = $xmlmcOutput.params.owner $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.outcomes -and $xmlmcOutput.params.outcomes -ne ""){ $outcomes = $xmlmcOutput.params.outcomes $requestWarnings = $xmlmcOutput.params.warnings } if($xmlmcOutput.params.taskAnswers -and $xmlmcOutput.params.taskAnswers -ne ""){ $taskAnswers = $xmlmcOutput.params.taskAnswers $requestWarnings = $xmlmcOutput.params.warnings } } # Build resultObject to write to output $resultObject = New-Object PSObject -Property @{ Status = $xmlmcOutput.status Error = $xmlmcOutput.error ExceptionName = $exceptionName ExceptionSummary = $exceptionSummary title = $title options = $options createdOn = $createdOn createdBy = $createdBy owner = $owner outcomes = $outcomes taskAnswers = $taskAnswers } if($resultObject.Status -ne "ok" -or $exceptionName -ne ""){ Write-Error $resultObject } else { $taskAnswers[0] | Select value }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now