經過Powershell 發送微信消息

前期須要註冊一個微信企業號(度娘能夠找到好多帖子)這部分就再也不詳細說了。shell

主要經過Powershell 調取微信企業號API來實現發送消息的目的。json

下面是一個寫好的函數你們能夠直接調用:api

function send-weixin {
Param(
        [Parameter(Mandatory = $True, Position = 1)]
        [String]$Username,
        [Parameter(Mandatory = $True, Position = 2)]
        [String]$Content
      )
$auth_string = https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=【你本身的Corpid】&corpsecret= 【你本身的密碼】
$auth_values = Invoke-RestMethod $auth_string
# Send message 下面是微信JSON內容的寫法
$token = $auth_values.access_token
$body="{
`"touser`":`"$username`",
`"msgtype`":`"text`",
`"agentid`":`"1`",
`"text`":
{`"content`":`"$content`"},
`"safe`":`"0`"
}"
$chinese=[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 $chinese
}
相關文章
相關標籤/搜索