1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellからSlackのにIncoming Webhooksを使って投稿

Posted at

PowerShell の場合、内部は UTF-16 なので、UTF-8 に変換してリクエストを投げる必要がある。
ネットで調べても正しい情報が見つからなかったが、いくつか試行錯誤の結果わかった。

ConvertTo-Json した後、[System.Text.Encoding]::UTF8.GetBytes で変換すればよい。

$output = 何かの処理の結果の配列など
$webhook = "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"

$message = $output -join "`n"
$payload = @{ 
    channel = "#random";
    text = $message;
}
$json = ConvertTo-Json $payload
$body = [System.Text.Encoding]::UTF8.GetBytes($json)
Invoke-RestMethod -Uri $webhook -Method Post -Body $body
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?