在服务器监控中之前都是发送邮件,但邮件有延时或有时没有看到不能实时发现报警,想改为微信接收报报警消息,在官网查看相关文档,powershell 实现代码如下:
function send-WeChat {
Param(
[String]$Content
)
[String]$corpid="企业ID"
[String]$pwd="Secret id"
[String]$GroupId="1"
[String]$AgentId="1000002"
$auth_string = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$pwd"
$auth_values = Invoke-RestMethod $auth_string
$token = $auth_values.access_token
$body="{
`"touser`":`"ChenShanLiang`",
`"agentid`":`"$AgentId`",
`"text`":{
`"content`":`"$content`"
},
`"msgtype`":`"text`"
}"
$To_CN=[System.Text.Encoding]::UTF8.GetBytes($body)
Invoke-RestMethod "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token" -ContentType "application/json" -Method Post -Body $To_CN
}
send-WeChat "TEST微信消息"