0
0

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 3 years have passed since last update.

Teams Webhook 特定の条件下で起きたエラーにハマった

Last updated at Posted at 2021-10-28

社内で定期実行しているPoweshellスクリプトがあって、TeamsのWebhookで通知を飛ばしたりしているんだけど、2021年10月下旬あたりからいきなり通知がこなくなったので、その調査記録をここに残す。

ローカルユーザーで実行するとエラーになる。

まずスクリプトの実行環境は以下の通り。

  • Network: 社内LAN(AcriveDirectoryあり)
  • 実行環境: Windows Server 2016
  • PSVersion: 5.1.14393.4583

普段はタスクスケジューラで定期実行しており、どのようなエラー(標準エラー出力)を吐いているのか全く把握できないので、まずはPoweshell上でスクリプトを実行してみた。因みにこの時はドメインユーザーで実行。
実際動かしてみると、エラー出力されずスクリプトが終了し、Teamsにもちゃんと通知が届いた。
「ん??ちゃんと動くじゃん。。」

今度はタスクスケジューラを手動実行してみた。
タスクスケジューラ自体は正常に終了したが、やはりTeamsに通知が来ない。。

ここでタスクスケジューラを実行するユーザー(ローカルユーザー)に注目し、ローカルユーザーでスクリプトを実行してみた。そしたらInvoke-RestMethodコマンドで以下エラーが吐き出された。

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At C:\filename.ps1:74 char:1
+ Invoke-RestMethod -Uri $WebHookURI -Method Post -ContentType 'applica ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

上記エラー内容を見ても正直原因追及ができず、そもそもなぜローカルユーザーだけいきなり使えなくなったんだ?プロファイル環境に依存なのか?

とりあえずこの日はローカルユーザー環境で起きることがわかったので、一旦終了した。。

原因はTLSのバージョン?

とりあえずエラーメッセージを検索窓にそのまま貼り付けてみたらPowershell の default の TLS 1.0利用しようとしてエラーを吐いているのでは?という英語の記事を見つけた。
記事の内容とおり以下コードをスクリプトファイルに追加してみたら、ローカルユーザーでもTeamsにWebhook通知が届くようになった。

Set-ExecutionPolicy Bypass -Scope Process -Force
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

ドメインユーザー、ローカルユーザーのPowershell 上の既定のTLSの値は同値(SSL3、TLS(1.0))なのに
なぜローカルユーザーだけエラーが起きてしまうのは正直謎のまま。。

もっと原因特定できたら追記します。🙇‍♂️🙇‍♂️

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?