捐血一袋救人一命

江蘇拙政園

江蘇 拙政園

全家福

日本 和歌山城

賞楓之旅

千燈 夕照

水鄉千燈

蘆洲 微風運河

破曉時分

顯示具有 Synology Chat Webhook 標籤的文章。 顯示所有文章
顯示具有 Synology Chat Webhook 標籤的文章。 顯示所有文章

2021年7月2日 星期五

使用 Powershell 發送 Synology Chat 訊息



Function Send-Chat{

    Param(

        [string] $Msg

    )

    Add-Type -AssemblyName System.Web

    $url = "http://your ip:port/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=%22el3fotmx3xHbiXGz17aTTXFUJ5DTzgun6bwhLA5bfqso%22"

    $json = 'payload={{"text": "{0}"}}' -f [System.Web.HttpUtility]::UrlEncode($Msg)

    Try{

        $Response = Invoke-WebRequest -Uri $url -Method POST -Body $json

    }Catch [exception]{

        $_

    }

}



Send-Chat ("{0:yyyy-MM-dd HH:mm:ss} DB Server CPU Loading {1:f0}%, Memory Loading {2:f0}%" -f (Get-Date), $CPUSensor, $MemSensor)

設定 $url 請參閱 https://kb.synology.com/zh-tw/DSM/tutorial/How_to_configure_webhooks_and_slash_commands_in_Chat_Integration#x_anchor_id5


要注意的是,當多台電腦,使用同一個 Webhook 傳送訊息時,只會處理第一個傳送到的
例如:我有 DB Server 1 & DB Server 2 跑相同的 Powershell 程式(控制同一時間同時傳送),就只會有其中一台的訊息會顯示