将 Slack CURL 调用更改为 PowerShell

Changing Slack CURL call into PowerShell

我正在编写一个从 Bash 到 Powershell 的工作 Slack 脚本的翻译,我偶然发现了问题的实质;如何用Invoke-WebRequest代替curl

这是在 *nix 系统上 Bash 中成功运行的 curl 命令:

curl \
-X POST \
-H "Content-type: application/json" \
--data "{
   \"attachments\": 
   [
      {
         \"mrkdwn_in\": [\"text\"],
         \"color\": \"$COLOUR\",
         \"text\": \"$MESSAGE\",
      }
   ]
}" \
https://hooks.slack.com/services/tokenpart1/tokenpart2

注意 $COLOUR 和 $MESSAGE 变量是从脚本的其他地方派生的(不是我遇到问题的部分)。

我无法让它在 PowerShell 中运行。我目前的翻译是:

$Body = @{
   "attachments" = "[
      {
         "mrkdwn_in": ["text"],
         "color": "$Colour",
         "text": "$Message",

   ]"
}

$BodyJSON = $Body |convertto-JSON

Invoke-WebRequest -Headers -Method Post -Body "$BodyJSON" -Uri "https://hooks.slack.com/services/tokenpart1/tokenpart2" -ContentType application/json

这会导致以下错误:

At C:\path-to-file-etc.ps1:53 char:11
+          "mrkdwn_in": ["text"],
+           ~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'mrkdwn_in": ["text"],
         "color": "$COLOUR",
         "text": "$MESSAGE",
      }
   ]"' in expression or statement.
At C:\path-to-file-etc.ps1:53 char:11
+          "mrkdwn_in": ["text"],
+           ~
The hash literal was incomplete.At 
C:\path-to-file-etc.ps1:53 char:11
+          "mrkdwn_in": ["text"],
+           ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : UnexpectedToken

Process exited with code 1
Process exited with code 1
Step Notify (PowerShell) failed

我对 Powershell 的经验几乎为零。也因为这个脚本必须能够放到各种各样的盒子上,我不会使用任何库或自定义 cmdlet 或类似的东西。 开箱即用的方法或死掉。

由于您无论如何都在使用 ConvertTo-Json,因此将 整个 输入构造为(嵌套的)自定义对象/哈希表 更简单,并且让 ConvertTo-Json 处理所有 JSON 格式 :

$Body = [pscustomobject] @{
  attachments = , [pscustomobject] @{
    mrkdwn_in = , 'text'
    color = $Colour
    text = $Message
  }
}

$BodyJson = $Body | ConvertTo-Json -Depth 3

注意:您可以用 [ordered] 代替 [pscustomobject] 来使用带有有序键的哈希表;即使省略其中任何一个都可以,尽管以不同的顺序查看结果 JSON 中的条目可能会造成混淆。

注意 , 运算符 array-construction 的使用,以确保 attachmentsmrkdwn_in 条目被视为数组。

此外,由于 ConvertTo-Json 仅完全序列化到 2 级别的默认深度 ,因此必须使用 -Depth 3 来确保条目 mrkdwn_in 的值被序列化为数组。


至于你试过的

你的直接问题(除了 Jeff Zeitlin 在对该问题的评论中指出的缺失 } ): 您忽略了 转义 嵌入的 " 个字符。 在您的 multi-line 字符串。 因此,正如文档主题 Get-Help about_Quoting_Rules 所讨论的,您可以使用 `""..." 中嵌入双引号或使用 here-string (@"<nl>....<n>"@).

即使修复了语法问题,您的代码也不会按预期工作,但是,因为 ConvertTo-Json 不会保留 pre-formatted JSONattachments 条目中,而是将字符串值视为字符串 literal 需要转义;这是一个简单的例子:

@{ 
    foo = "[
      1, 
      2 
    ]"
 } | ConvertTo-Json

以上结果:

{
  "foo": "[\n    1, \n    2 \n   ]"
}