You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Automation Webhook无法在PowerShell中传递参数求助

问题描述

在Azure自动化账户中部署了PowerShell脚本Runbook(测试过PowerShell 5.1和7.2版本),脚本参数定义如下:

#set arguments
param(
    [string]$Origin,
    [string]$title,
    [string]$Description
)

直接在Azure中执行脚本时可正常传入参数,但通过以下PowerShell脚本触发Runbook的Webhook并传递参数时,脚本无法解析参数:

# Define the parameters
$params = @{
    "ORIGIN" = "value1"
    "TITLE" = "value2"
    "DESCRIPTION" = "value3"
}

# Convert the parameters to a JSON string
$jsonParams = $params | ConvertTo-Json

try {
    # Invoke the webhook
    Invoke-RestMethod -Method Post -Uri $webhookUrl -Body $jsonParams -ContentType "application/json"
} catch {
    Write-Host "Error: $_"
}

Runbook日志显示Webhook已触发,输入内容如下:

{"WebhookName":"CreateItem","RequestBody":"{\r\n \"TITLE\": \"value2\",\r\n \"DESCRIPTION\": \"value3\",\r\n \"ORIGIN\": \"value1\"\r\n}","RequestHeader":{"Connection":"Keep-Alive","Host":"0117e8aa-9b60-4cb6-b844-2f558f86d135.webhook.we.azure-automation.net","User-Agent":"Mozilla/5.0","x-ms-request-id":"cdb71f35-c396-4f05-801a-b4cc155c146b"}}

但脚本输出参数时结果均为空:

Using as my the origin
Using as the title
Using the following description:

解决方法

1. 修正参数大小写匹配问题

Azure自动化Runbook的参数名区分大小写,当前传递的参数是全大写(ORIGIN、TITLE、DESCRIPTION),但Runbook定义的参数是混合大小写($Origin、$title、$Description),两者不匹配导致无法映射。

修改触发脚本中的参数名,与Runbook定义保持一致:

# Define the parameters (匹配Runbook的参数大小写)
$params = @{
    "Origin" = "value1"
    "title" = "value2"
    "Description" = "value3"
}

# Convert the parameters to a JSON string
$jsonParams = $params | ConvertTo-Json

try {
    # Invoke the webhook
    Invoke-RestMethod -Method Post -Uri $webhookUrl -Body $jsonParams -ContentType "application/json"
} catch {
    Write-Host "Error: $_"
}

2. 在Runbook中显式解析Webhook请求体

如果希望保持参数传递的灵活性,可在Runbook中直接读取Webhook的请求体并解析,不依赖自动参数映射。修改Runbook脚本如下:

# 读取并解析Webhook请求体
$webhookInput = $WebhookData.RequestBody | ConvertFrom-Json

# 提取参数
$Origin = $webhookInput.Origin
$title = $webhookInput.title
$Description = $webhookInput.Description

# 后续业务逻辑示例
Write-Output "Using $Origin as my the origin"
Write-Output "Using $title as the title"
Write-Output "Using the following description: $Description"

注意:$WebhookData变量仅在Webhook触发Runbook时存在,需确保Runbook已启用Webhook支持。

验证要点

  • 确认Webhook请求体的JSON参数名与Runbook定义完全匹配(包括大小写)
  • 在Runbook中临时输出$WebhookData的完整内容,排查输入是否正确接收
  • 检查Runbook日志中的RequestBody字段,确认参数结构无误

内容的提问来源于stack exchange,提问作者vulkoek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 10:10:13