azure - 从 ADF Webhook 事件调用 Azure 自动化 Runbook - 失败,显示 "Cannot bind argument to parameter ' InputObject',因为它为空。”

标签 azure powershell azure-data-factory webhooks

我们正在尝试从 Azure 数据工厂管道中的 Webhook 事件调用 Azure 自动化 Runbook。

我们按照此博文中提供的说明进行工作:https://medium.com/hitachisolutions-braintrust/3-steps-to-run-powershell-in-azure-data-factory-e7c73d38e548 .

涉及两个组件:

  1. Azure 自动化 Runbook,包含 PowerShell 脚本
  2. 调用 Automation Runbook 的 ADF 管道 Webhook 事件

目的是读取 PS 脚本的输入参数 (WebhookData) 中包含的属性 (RequestBody),该属性表示 json 格式的字符串。

为了清楚起见,我删除了尽可能多的代码(实际脚本包含更多行)。例如,我删除了对回调等的任何引用...因为(此时)这不是我们面临的问题的一部分。

此外,我还描述了自动化运行手册的 2 个连续版本,以演示我们迄今为止如何尝试解决该错误。

自动化操作手册(初始版本)

Runbook 的 PowerShell 代码:

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

$Parameters = (ConvertTo-Json -InputObject $WebhookData.RequestBody) // This line fails

Webhook 事件

调用自动化 Runbook 的 Webhook 事件的 json 定义:

{
"name": "Automation Runbook Test",
"properties": {
    "description": "Performs a simple test to call a runbook from within ADF",
    "activities": [
        {
            "name": "WebHook1",
            "type": "WebHook",
            "dependsOn": [],
            "userProperties": [],
            "typeProperties": {
                "url": "https://xxxxxxxx-94be-4cb8-9ba7-b50d7de44a82.webhook.we.azure-automation.net/webhooks?token=xxxxxxxxxx6bjpZH36io1mhP%2b5k2yr%2bMcVvsYGjdZPE%3d",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": {
                    "ParmMsg": "I am completely operational, and all my circuits are functioning perfectly."
                },
                "timeout": "00:00:30"
            }
        }
    ],
    "folder": {
        "name": "Refresh Power BI"
    },
    "annotations": []
},
"type": "Microsoft.DataFactory/factories/pipelines"
}

执行错误

执行时,自动化 Runbook 失败并出现以下错误:无法将参数绑定(bind)到参数“InputObject”,因为它为空。

自动化操作手册(第二版)

为了了解正在发生的情况,我们尝试了几种方法。我们注意到,将 WebhookData 参数转换为 PSCustomObject 似乎让我们更近了一步,但当我们尝试继续前进时,我们遇到了下一个问题。这就是我们目前陷入困境的地方。

这是新版本:

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

# Convert WebhookData parameter (json-formatted string) to an Automation.PSCustomObject object
$WebHookData = (ConvertFrom-Json -InputObject $WebhookData)
Write-Output "WebhookData.RequestBody"
Write-Output "-----------------------"
Write-Output $WebhookData.RequestBody

# Get all parameters from body 
# (passed from Data Factory Web Activity)
$Parameters = (ConvertFrom-Json -InputObject $WebhookData.RequestBody) // This line fails

Write-Output "Parameters"
Write-Output "----------"
Write-Output $Parameters

输出和错误(来自第二个版本)

$WebhookData.RequestBody 的写入输出显示,与 ADF Webhook 事件设置中输入的原始 json 相比,转义字符 (\r\n) 已添加到 RequestBody 属性中(参见上文):

WebhookData.RequestBody
-----------------------
{\r\n "ParmMsg": "I am completely operational, and all my circuits are functioning perfectly.",\r\n "callBackUri": "https://dpwesteurope.svc.datafactory.azure.com/dataplane/workflow/callback/....

脚本现在失败并显示
从 JSON 转换失败并出现错误:属性标识符无效 |特点: 。路径 '',第 1 行,位置 1。

当(手动)删除转义字符时,脚本运行正常。然而,这些转义字符是由 ADF Webhook 事件放置在那里的,似乎没有办法控制它。我也不认为那里不应该改变。 相反,自动化 Runbook 脚本可能应该进行更改,以便它将 RequestBody 属性(外部 WebhookData 对象的)识别为有效的 json,可以将其转换为 PSCustomObject。如图所示,我们的脚本此时显然无法做到这一点。

非常欢迎任何建议。

谢谢!

自原始帖子以来更新

我将 Runbook 的运行时版本从 7.1(预览版)更改为 5.1。在 5.1 版本中,脚本似乎可以正确运行。那么,是 7.1 版本的原因,还是我在这里遗漏了其他内容?

最佳答案

事实证明,Powershell 7.1(预览版)目前存在一个已知问题:

当您使用 Webhook 启动 PowerShell 7 Runbook 时,它会自动将 Webhook 输入参数转换为无效的 JSON。

来源:https://learn.microsoft.com/en-us/azure/automation/automation-runbook-types#known-issues---71-preview

关于azure - 从 ADF Webhook 事件调用 Azure 自动化 Runbook - 失败,显示 "Cannot bind argument to parameter ' InputObject',因为它为空。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71681009/

相关文章:

json - Powershell 转换为 Json 格式错误

Azure 数据工厂 V2 : Copy OR Stored Procedure activity for SQL merge

python - Azure ML 中混淆矩阵中每个单元格的项目数的浮点值

silverlight - 从 Silverlight 访问声明感知 WCF

azure - 升级 Databricks premium 后无法启动集群 - ssh 错误

Azure 应用服务 Multi-Tenancy 功能实际上不起作用

azure - 用于检索 Azure 订阅名称、资源组、资源名称、资源类型、标签和位置的 KQL 查询

linux - azure 无法从 vhd 镜像创建 vm

azure-devops - 具有改进的 CI/CD 的 Azure 数据工厂部署

oracle - 使用 Azure 数据工厂按日期动态查询 Oracle 表