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

Eric Neiner 51 Reputation points
2023-03-28T17:13:28.87+00:00

Azure Automation Powershell 5.1 Runbook unable to get json payload.

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 asap. So, I wanted to 5.1 for now.

What am I doing wrong?

Payload

[{
	"Alias": "jack", 
	"User" : "john"
}]

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

User's image

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;
    }
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,234 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,444 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Eric Neiner 51 Reputation points
    2023-03-28T19:07:40.5466667+00:00

    Here is the fix.

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

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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.