MSSQL/WMI/PowerShell结合篇(四)PowerShell发送微信信息

本文介绍如何通过PowerShell发送微信信息

目前创新互联已为上千多家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器租用、企业网站设计、临西网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

先申请企业微信(企业、政府、组织三种类型),通过PowerShell调用企业微信的API群发接口发送微信信息,API详细信息可参考企业微信开发者文档

以下为PowerShell调用企业微信API发送文本信息

----Script File: send_WeChat.ps1

param($param1,$param2,$param3)

##设置16-32位长度的密钥

function Set-Key {

param([string]$string)

$length = $string.length

$pad = 32-$length

if (($length -lt 16) -or ($length -gt 32)) {Throw "String must be between 16 and 32 characters"}

$encoding = New-Object System.Text.ASCIIEncoding

$bytes = $encoding.GetBytes($string + "0" * $pad)

return $bytes

}

##解密方法

function Get-EncryptedData {

param($key,$data)

$data | ConvertTo-SecureString -key $key |

ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))}

}

##发送微信方法

function send_WeChat_To_DBA {

Param(

        [String]$corpid,

[String]$pwd,

        [String]$Content

      )

$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="{ 

   `"toparty`":`"receive group id`",

   `"agentid`":`"your agent id`",

   `"text`":{

      `"content`":`"$content`"

   },

   `"msgtype`":`"text`"

}"

$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 $CN

}

##解析帐号密码

$cidstr= Get-Content E:\Monitor\cidstr.txt;

$pwdstr = Get-Content E:\Monitor\keystr.txt;

$cidkey = Set-Key $cidstr;

$pwdkey = Set-Key $pwdstr;

$cidfromFile = Get-Content E:\Monitor\wxcid.txt;

$pwdfromFile = Get-Content E:\Monitor\wxpwd.txt;

$corpid = Get-EncryptedData -data $cidfromFile -key $cidkey;

$pwd = Get-EncryptedData -data $pwdfromFile -key $pwdkey;

$content=$param1+$param2+$param3

##调用发送方法

send_WeChat_To_DBA -corpid $corpid -pwd $pwd -Content $content

##send_WeChat.ps1调用方式

E:\Monitor\send_WeChat.ps1 $param1 $param2 $param3


标题名称:MSSQL/WMI/PowerShell结合篇(四)PowerShell发送微信信息
文章出自:http://bzwzjz.com/article/pjceee.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 定制网站建设 盐亭网站设计 网站设计 成都企业网站建设 泸州网站建设 外贸网站建设 成都网站制作 成都网站建设 教育网站设计方案 重庆手机网站建设 重庆企业网站建设 成都网站建设公司 营销型网站建设 成都定制网站建设 攀枝花网站设计 营销网站建设 做网站设计 四川成都网站建设 网站建设改版 企业网站建设 商城网站建设 网站设计制作