简体   繁体   中英

Azure Automation Runbook fails triggered with Webhook "Missing argument in parameter list"

I have a fairly simple script placed in Azure Automation Account that Stops Azure Container Instances. I runs smoothly from the portal when I test it or when I just click start button producing no errors and doing the job correctly. '''

Param
(
  [parameter (Mandatory=$false)]
  [object]$WebhookData
)

try
{
    "Logging in to Azure..."
    Connect-AzAccount -Identity
}
catch {
    Write-Error -Message $_.Exception
    throw $_.Exception
}

$ACIs = Get-AzContainerGroup

foreach ($ACI in $ACIs)
{    
    Write-Output ("Shutting down the following Azure Container Instance:  " + $ACI.Name)
    Stop-AzContainerGroup -Name $ACI.Name -ResourceGroupName 'MY_RESOURCE_GROUP_NAME'
    Write-Output ("")
}

I created a webhook to start that script. Whenever I sent post request to fire that script it fails with the following error: '''

ParserError:
Line |
| … .ps1' -WebhookData {WebhookName:XXXX,RequestB …
| ~
| Missing argument in parameter list.

I tried adding sth to message body, sending request from PowerShell and Postman. I am getting response back with JobIds and status code 202. I was looking for similar problem but the only one I got is that one without an aswer.

Any ideas?

The problem was with Powershell version in Azure Automation Runbook. Using Powershell 5.1 solved the problem. It looks like that when Runbook is using Powershell 7.1 it has some formatting problems: link .

It appears the $WebhookData variable contains invalid JSON. The JSON is missing all the double quotes around names and values.

I wrote an ugly regex to "fix" the $WebhookData value, adding back in the missing double quotes. This allows me to pass the string through ConvertFrom-JSON and get a usable object.

I'm sure the regex isn't perfect, but it works for my specific case, so I'm running with it for now.

    $Payload = $WebhookData -replace "(?<pre>[\{,])(?<name>[^:]*)", '${pre}"${name}"' -replace ":(?<value>[^\{\},]+)", ':"${value}"' | ConvertFrom-Json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM