我在 Azure 自动化帐户中有一个 powershell 脚本 Runbook(使用 Powershell 5.1 和 7.2 进行测试)。
此脚本需要参数:
#set arguments
param(
[string]$Origin,
[string]$title,
[string]$Description
)
我可以直接在 Azure 中执行脚本并提供我的参数。但是,当我运行脚本来触发 Runbook 的 Webhook 并提供参数时,脚本中不会解析任何参数。我通过以下 powershell 脚本执行此操作:
# 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"}}
当我在脚本中记录参数时,如下所示:
Write-Output "Using $Origin as my the origin"
Write-Output "Using $title as the title"
Write-Output "Using the following description: $Description"
然后这是输出:
作为我的原点
用作标题
使用以下描述:
我不知道该怎么办,请帮忙:)
trigger_webhook.ps1
$Params = @(
@{ Origin="Hawaii"},
@{ title="Seattle"},
@{ Description="Florida"}
)
$body = ConvertTo-Json -InputObject $Params
$webhookURI = "https://xxxxxxx.webhook.eus.azure-automation.net/webhooks?token=xxxxxxxxjkJlQ%3d"
$response = Invoke-WebRequest -Method Post -Uri $webhookURI -Body $body -UseBasicParsing
$response