Search code examples
powershellazure-automationazure-runbook

Azure Automation Powershell 5.1 Runbook - ConvertFrom-Json : Invalid JSON primitive


Azure Automation Powershell 5.1 Runbook unable to get json payload.

I am posting this in the body of the webhook request - I know this is correct.

This code works to get the parameters in 7.2 but I am using the exhangeonlinemanagement module which is having other issues in runbooks with 7.2. I need to get this working, so I wanted to use 5.1 for now.

What am I doing wrong?

All I need to do is get the parameters from the payload, but I keep getting this error:

ConvertFrom-Json : Invalid JSON primitive: . At line:11 char:31 + $Payload = $WebhookData | ConvertFrom-Json + ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException

  • FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
[{
    "Alias": "jack", 
    "User" : "john"
}]

Webhook request received by the runbook

This is the code in my RunBook

param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData
)

if ($WebhookData) {

    Write-Output $WebhookData.RequestHeader

    $Payload = $WebhookData | ConvertFrom-Json


    if ($Payload.RequestBody) { 

        $aliases = (ConvertFrom-Json $Payload.RequestBody)

            foreach ($x in $aliases)
            {
                $alias = $x.Alias
                $user = $x.User
                Write-Output "Alias: $alias"
                Write-Output "User: $user"
            }
    }
    else {
        Write-Output "Request Error"
    }


} else
    {
        Write-Output "Missing information";
        exit;
    }

Solution

  • Here is the fix.

    $Payload = $WebhookData | ConvertFrom-Json was not needed.

    I took this from the Microsoft example. So, I assumed this working.