LoginSignup
3
2

More than 5 years have passed since last update.

PowerShellでアプリの起動と終了をSlackへ投稿

Last updated at Posted at 2019-02-26

【Windows】incoming-webhookを利用してバッチファイル経由で投稿します

Windowsで特定のアプリを起動・終了するたびにslackにポスト。
curlとかnkfをダウンロードしなくてもいい方法を考えたけど、そっち使ったほうがバッチファイルのみで処理できます。

slack.ps1
Param(
    [string]$username,
    [string]$channel,
    [string]$message
)

function Push-Notification($username,$channel,$message) {
   $webhookuri = "https://hooks.slack.com/services/~~~"
   $channel = "#" + $channel
   $text = @{
             text = $message
             channel = $channel
             username = $username
             }| ConvertTo-Json -Compress
   $payload = [Text.Encoding]::UTF8.GetBytes($text)
   Invoke-RestMethod -Uri $webhookuri -Method Post -Body $payload
}
# >slack.ps1 "ユーザー名" "チャンネル名" "本文"
Push-Notification $username $channel $message

実行ポリシーを設定して実行

hoge.bat
powershell -WindowStyle Hidden -ExecutionPolicy Bypass -command .\slack.ps1 "わいです" "channel" "hogeが起動"
@echo off
call hoge.exe
powershell -WindowStyle Hidden -ExecutionPolicy Bypass -command .\slack.ps1 "わいです" "channel" "hogeが終了"

特定のアプリはバッチファイルから起ち上げるようにします

3
2
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
3
2