码迷,mamicode.com
首页 > 微信 > 详细

通过Powershell 发送微信消息

时间:2015-08-30 23:33:48      阅读:1026      评论:0      收藏:0      [点我收藏+]

标签:powershell   微信   

前期需要注册一个微信企业号(度娘可以找到好多帖子)这部分就不再详细说了。

主要通过Powershell 调取微信企业号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
}


本文出自 “运维人生” 博客,请务必保留此出处http://lixiaosong.blog.51cto.com/705126/1689886

通过Powershell 发送微信消息

标签:powershell   微信   

原文地址:http://lixiaosong.blog.51cto.com/705126/1689886

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!