PowerShell松弛功能

时间:2019-08-06 16:05:48

标签: powershell

我正在尝试编写PowerShell函数来调用Slack Webhook。我从Reddit post中获取了该函数,但是该函数似乎由于解析错误而失败。我也删除了webhook。

代码如下:

function Send-SlackMessage {
    Param (
        [Parameter(Mandatory=$true, Position=0)]$Text,
        $Url = "https://hooks.slack.com/services/xxxxx",
        # Parameters below are optional and will fall back to the default
        $Username = "XXXXXXX",
        $Channel = "XXXXXXX",
        $Emoji = "XXXXXX"
    )

    $body = @{ text=$Text; channel=$Channel; username=$Username; icon_emoji=$Emoji } | ConvertTo-Json
    Invoke-WebRequest -Method Post -Uri $Url -Body $body
}

和错误:

At line:12 char:67
+ ... y = @{ text=$Text; channel=$Channel; username=$Username; icon_emoji=$ ...
+
Missing '=' @{ text=$Text; channel=$Channel; username=$Username; icon_emoji=$ ...

The hash literal was incommplete.
 + CategoryInfo              :ParserError (:) [], ParentContainsErrorRecordException
 + FillyQualifiedErrorId     : MissingEqualsInHashLiteral

1 个答案:

答案 0 :(得分:0)

函数名称在哪里?在第二个参数之后您缺少逗号,而在最后一个参数之后您又有一个逗号。

function whatever ()
{
param (
     [Parameter(Mandatory=$true, Position=0)]$Text,
     $Url="https://hooks.slack.com/services/xxxxx",
     # Parameters below are optional and will fall back to the default
        $Username = "XXXXXXX",
        $Channel = "XXXXXXX",
        $Emoji = "XXXXXX"
    )

    $body = @{ text=$Text; channel=$Channel; username=$Username; icon_emoji=$Emoji } | ConvertTo-Json
    Invoke-WebRequest -Method Post -Uri $Url -Body $body
}