3
3

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投稿する

Posted at

Powershellで定時でSlack投稿する

想定読者

  • 会社でWindows環境しかない (cronが使えない)ひと
  • 会社の事情により, 仮想化環境でLinuxも利用できないひと
  • とりあえずWindowsだけですませたいひと
  • 定時でSlackに投稿することで、意識付けとかしたいひと

利用するツール

  • Powershell
  • タスクスケジューラ

Slackでの設定

基本的にはPowershellからSlackのIncomming webhookをコールして、投稿を実施する。
設定手順は下記の通り。(だいぶ端折ったが、様々な記事で公開されている無いようなので簡素にする)

  1. https://{workspace-name}.slack.com/appsにアクセスする
  2. Incoming Webhooksに移動し、Add cofigurationをクリック。適切な情報を入力してWebhookのURLを入手する。

Powershellの設定

Powershellは専用のScriptを書いて実行することが可能だが、少し設定をupdateする必要があり少しめんどくさい。
そこで、この記事ではスクリプトは利用せずに実行する。
また、Webhookをコールするためには、cURLではなくInvoke-RestMethodなるものを活用する。

Powershell上でのSlack webhookコール

下記のコマンドを実行することで、WebhookからSlackに投稿できることを確認する。

powershell -Command "Invoke-RestMethod -Method Post -Body \""{ 'text':'hello world!' }\"" -Uri \""https://{webhook-uri}"""

-Command移行を直接powershell上で実行することでも同様の実行はできるが、後続のタスクスケジューラではこの形式での呼び出しになるため、この方式でトライをする。

タスクスケジューラの設定

手順は下記

  1. タスクスケジューラを起動し、タスクの作成をクリック。
  2. 操作タブをクリック、新規ボタンを押下
  3. プログラム/スクリプトに%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exeを入力
  4. 引数の記載にpowershell -Command "Invoke-RestMethod -Method Post -Body \""{ 'text':'hello world!' }\"" -Uri \""https://{webhook-uri}"""を入力

以上で、定時実行でSlackにポストされるはずである。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?