Skip to content

Instantly share code, notes, and snippets.

@tknhs
Last active May 21, 2022 15:05
Show Gist options
  • Save tknhs/bc8554cbef4492a62650bb4943183e54 to your computer and use it in GitHub Desktop.
Save tknhs/bc8554cbef4492a62650bb4943183e54 to your computer and use it in GitHub Desktop.
ユーザセッションがアクティブでない場合にLINEへ通知を送る
<#
.SYNOPSIS
セッション状態を確認するスクリプト
.DESCRIPTION
対象ユーザのセッションがアクティブでない場合にLINEへ通知を送る
.EXAMPLE
PS C:\Users> .\session-checker.ps1 -token xxxx -server test-server -user Administrator
...
.PARAMETER token
LINE Token
.PARAMETER server
サーバ名
.PARAMETER user
セッション監視対象ユーザ名
<CommonParameters> はサポートしていません
#>
#################################################################################
Param(
[parameter(mandatory=$true)][string]$token,
[parameter(mandatory=$true)][string]$server,
[string]$user = "Administrator"
)
$state = $null
# rdp
$session = query session | select-string $user | foreach { -split $_ } | select -index 3
if ($session -eq "Active") {
$state = $session
}
# 切断
$session = query session | select-string $user | foreach { -split $_ } | select -index 2
if ($session -eq "Disc") {
$state = $session
}
# notify to line
if ($state -ne "Active") {
$uri = "https://notify-api.line.me/api/notify"
$header = @{Authorization = "Bearer $token"}
$body = @{message = "[$server] $user のセッションがアクティブでないためサーバを確認してください"}
Invoke-RestMethod -Uri $uri -Method POST -Headers $header -Body $body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment